RhymeGuy
RhymeGuy

Reputation: 2112

jQuery - cannot get variable inside hover function (mouseleave)

I need to pass value of current_src to mouseleave function.

console.log returns undefined.

var current_src, swap_src;
jQuery(".browseProductImage").hover(
    function(){
        var current_src = jQuery(this).attr('src');
        var swap_src = jQuery(this).next().attr('src');
        jQuery(this).attr('src', swap_src); 
    },
    function(){
        jQuery(this).attr('src', current_src);
        console.log(current_src)
    }
);

Upvotes: 0

Views: 731

Answers (1)

A. Wolff
A. Wolff

Reputation: 74420

Remove the second var for current_src, you want to use here the upper scoping one variable:

var current_src, swap_src;
jQuery(".browseProductImage").hover(
    function(){
        current_src = jQuery(this).attr('src');
        var swap_src = jQuery(this).next().attr('src');
        jQuery(this).attr('src', swap_src); 
    },
    function(){
        jQuery(this).attr('src', current_src);
        console.log(current_src)
    }
);

Upvotes: 1

Related Questions