March 3, 2013

keep current src attr so I can retrieve image

Question by mt0s

Code is like this :

$(' html body div.wrapper div.middle div.post_home:first-child a ').hover(
function() {
        $(' html body div.wrapper div.middle div.post_home:first-child a > img ').attr("src", "http:/site/test1.png");
},
 function() {
        $('html body div.wrapper div.middle div.post_home:first-child a > img ').attr("src", "currentSrc");
});

I just need to keep the src value of the image before the change so on mouseleave I can retrieve the image and display it.

Answer by Starx

Backup your src attrivute at the start of your event before changing it. And use it when you want to go to back.

var currentSrc;
$('.post_home:first-child a ').hover(function() {

    currentSrc = $('.post_home:first-child a > img').attr("src");
    $('.post_home:first-child a > img ').attr("src", "http:/site/test1.png");

}, function() {

        $('.post_home:first-child a > img').attr("src", currentSrc);

});

Note: I edited your selectors because what you have is more than necessary.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!