1

I have the following output.

<ul id='centrelist'>
<li class="centreicon"><a href="admin/manage_mycentre/22"><img width='70' height='70' class='centreimg' src="http://localhost/aktivfitness_new/assets/images/centre/centre1.jpg" /></a><div class="buttons"><a href="admin/manage_mycentre/22">Aktiv trening Arendal</a></div></a></li>
<li class="centreicon"><a href="admin/manage_mycentre/16"><img width='70' height='70' class='centreimg' src="http://localhost/aktivfitness_new/assets/images/centre/centre2.jpg" /></a><div class="buttons"><a href="admin/manage_mycentre/16">Aktiv trening Blindheim</a></div></a></li>
<li class="centreicon"><a href="admin/manage_mycentre/17"><img width='70' height='70' class='centreimg' src="http://localhost/aktivfitness_new/assets/images/centre/centre3.jpg" /></a><div class="buttons"><a href="admin/manage_mycentre/17">Aktiv trening Eid</a></div></a></li>
...

Now when I hover any image, I want to change it to hover.jpg with jquery.

I have the following but it is not working.

$(function() {
    $(".centreimg")
        var orgimg = $(this).attr("src");
        .mouseover(function() {
            var hoverimg = $(this).attr("src").match(/[^\.]+/) + "hover.jpg";
            $(this).attr("src", hoverimg);
        })
        .mouseout(function() {
            $(this).attr("src", orgimg);
        });
});
2
  • Any reason you aren't using CSS background images, and then changing it on hover using the :hover pseudo selector? Commented May 10, 2011 at 16:36
  • I am with @rich-bradshaw, css approach for this case sounds as the best option Commented May 10, 2011 at 16:46

2 Answers 2

1

just a minor correction

$(function() {
    $(".centreimg").mouseover(function() {
            var hoverimg = $(this).attr("src").match(/[^\.]+/) + "hover.jpg";
            var orgimg = $(this).attr("src");

            $(this).attr("src", hoverimg);
            $(this).attr("title", orgimg);
        })
        .mouseout(function() {
            $(this).attr("src", $(this).attr("title"));
        });
});

You have to store data within object

Sign up to request clarification or add additional context in comments.

Comments

0

jQuery code using the data() method, it makes it simpler.

$('.centreimg').hover( 
function() {
    $(this).data('src',$(this).attr('src'));
    $(this).attr('src', 'hover.jpg' ); //or hoverimg if it is declared
},
function(){
    $(this).attr('src', $(this).data('src') );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.