Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  58] [ 7]  / answers: 1 / hits: 152219  / 12 Years ago, tue, may 22, 2012, 12:00:00

What's the correct way to change an image on mouseover and back on mouseout (with/without jQuery)?



<a href=# id=name>
<img title=Hello src=/ico/view.png onmouseover=$(this).attr('src','/ico/view.hover.png') />
</a>


Ok, this is working, but how to change back to the original image after mouseout?



If it is possible, I want to do this thing inline, without document.ready function.


More From » jquery

 Answers
153

Try something like this:



HTML:



<img src='/folder/image1.jpg' id='imageid'/>


jQuery:



$('#imageid').hover(function() {
$(this).attr('src', '/folder/image2.jpg');
}, function() {
$(this).attr('src', '/folder/image1.jpg');
});




EDIT: (After OP HTML posted)



HTML:



<a href=# id=name>
<img title=Hello src=/ico/view.png/>
</a>


jQuery:



$('#name img').hover(function() {
$(this).attr('src', '/ico/view1.png');
}, function() {
$(this).attr('src', '/ico/view.png');
});



[#85411] Monday, May 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;