I need to change the src attribute of the image when the link is being hover on
<div class="clear span-33 last" id="navigation">
<div class="hicon span-1"><a href="#" title="Homepage"><img src="../Assets/images/home.png" /></a></div>
</div>
Also change it to default when the link is not hovered on...
You really should look into using CSS sprites for switching backgrounds on hover. But if you need to do this in jQuery, something like this should do it. Just change the over source image to your liking (also preloads the hover image):
var link = $('a'),
img = link.children('img'),
orig = img.attr('src'),
over = 'over.png',
temp = new Image();
temp.src = over; // preloads
link.hover(function() {
img.attr('src',over);
},function() {
img.attr('src',orig);
}
I understand sometimes you can't just swap the background position, so I too was looking for this with a IMG based Navigation (works too just rmbr to load jquery):
<script type="text/javascript">
$(document).ready(function(){
$(".navbar li").each(function(){
var link = $(this).children("a");
var image = $(link).children("img");
var imgsrc = $(image).attr("src");
// add mouseover
$(link).mouseover(function(){
var on = imgsrc.replace(/.jpg$/gi,"_over.jpg");
$(image).attr("src",on);
});
// add mouse out
$(link).mouseout(function(){
$(image).attr("src",imgsrc);
});
});
});
</script>
<ul class="navbar"><li><a href="#"><img src="/images/nav_home.jpg" alt="home" /></a></li>
<li><a href="#"><img src="/images/nav_item2.jpg" alt="Item2" /></a></li>
<li><a href="#"><img src="/images/nav_item3.jpg" alt="Item3" /></a></li>
</ul>
This question may help: img src & jQuery?
This should do it.
$('a[title="Homepage"]').hover(
function () {
// this is the mouseon event
$(this).children('img').attr('src', 'newimage.png');
},
function () {
// this is the mouseout event
$(this).children('img').attr('src', '../Assets/images/home.png');
}
);
This is my approach and it works but look too weird and made me a novice of jQuery
$('.hicon > a').hover(
function(){
$(this).html("<img src='../Assets/images/homeah.png' />");
},
function(){
$(this).html("<img src='../Assets/images/home.png' />");
}
);
I believed there is a better approach. Thanks