Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  48] [ 3]  / answers: 1 / hits: 84460  / 13 Years ago, sat, january 28, 2012, 12:00:00

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.


More From » jquery

 Answers
66

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.



JS



$(#id1).fadeOut();
$(#id2).delay(200).fadeIn();


HTML



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


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


[#87760] Thursday, January 26, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;