Bobby Francis Joseph
Bobby Francis Joseph

Reputation: 600

Specifying a new source for the image tag at runtime in Javascript , jQuery

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/

Upvotes: 0

Views: 1426

Answers (3)

mcgrailm
mcgrailm

Reputation: 17640

This should do the trick:

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

Upvotes: 1

Amritpal Singh
Amritpal Singh

Reputation: 1785

I have updated your fiddle

Upvotes: 1

Steen
Steen

Reputation: 2877

Try this

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

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

Upvotes: 3

Related Questions