Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery hover a link swap image

Tags:

jquery

image

swap

I want to do the follow, if a user hover on a link that the image will swap with a image with '-on' at the end.

But how can i get the swap thing on the image when i hover the a tag?

HTML code:

<div>
    <a href="#">
        <img src="image.jpg" alt="" />
        Here some caption
    </a>
</div>

I can not place the image urls in the header...

like image 915
Maanstraat Avatar asked Nov 28 '22 16:11

Maanstraat


1 Answers

$(function () {
    $('a img').hover( function () {
        $(this).attr('src', $(this).attr('src').replace(/\.jpg/, '-on.jpg') );
    });
});

Read the jQuery docs on replace and attr.

like image 67
hjpotter92 Avatar answered Dec 05 '22 17:12

hjpotter92