0

Is it possible to change the src of a image at runtime using jQuery. I have created a fiddle. I am trying to load the image specified in the variable $newsrc when the button is clicked. I am not sure as to what should be specified an argument function changeImage(). I do not want to use anonymous function.

http://jsfiddle.net/bobbyfrancisjoseph/KMFj2/5/

2
  • Sorry. Forgot to add fiddle. I have edited and added it. Commented Mar 30, 2012 at 12:27
  • want to something like this : jsfiddle.net/KMFj2/8 Commented Mar 30, 2012 at 12:29

3 Answers 3

3

Try this

   $("#myimg").attr('src', "/myNEWimage.jpg");

where ofcourse you get the path specified from somewhere in a variable

Sign up to request clarification or add additional context in comments.

1 Comment

jsfiddle.net/KMFj2/13 - Note the change in when the jquery is loaded (didn't work with the previous setting - should resemble real world use though)
1

I have updated your fiddle

1 Comment

Please update your answer such that the Q and A can be read and understood in the thread. It's better to copy a (small) piece of code here that explains what you did and why you did it. Now it's unfortunately impossible for others to read and understand the solution...
1

This should do the trick:

$('#image_id').click(function() {
     $(this).attr('src', "/new_image.jpg");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.