Satch3000 Satch3000 - 1 year ago 72
jQuery Question

JQuery change the value of an <img src="" by ID

I need some simple JQuery code so I can change the src value of a specific img.

It's currently:

<img id="myImage" src="image1.gif" />

and I need to change it to:

<img id="myImage" src="image2.gif" />

using JQuery.

Answer Source

Using: $(function(){ ... });

You can use:

$('#id').attr('src', 'newImage.jpg');

to change the image source immediately.

Alternatively, you can use jQuery animations to change an image.




    <img id='id1' src='one.jpg'>
    <img id='id2' src='two.jpg'>

(Don't forget to change the CSS of #id2 and put display: none as initial state).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download