0

I'm doing a very simple hover function for a button and I need to pas down a variable. I can use the variable in the first state of the hover but not in the second. Does it have something to do with the fact that the variable is defined locally and not available in within that second function?

Here's the code:

$('#'+'[id^="world_map"]').hover(function() {

    var that2 = this.id;

    $('#' +that2+ '_thumbnail_container').animate({"opacity" : 1}, 150, function() { });
    $('#' +that2+ '_thumbnail_container').css('visibility','visible');

        },                  


    function() {
    $('#' +that2+ '_thumbnail_container').animate({"opacity" : 0}, 150, function() { });

    $('#' +that2+ '_thumbnail_container').css('visibility','hidden');
});

thanks!

1 Answer 1

2

You're right, it is because the variable that2 is defined in the scope of the first function. However, the value (this.id) itself is available in both methods.

$('#'+'[id^="world_map"]').hover(function() {


    $('#' +this.id + '_thumbnail_container').animate({"opacity" : 1}, 150, function() { });
    $('#' +this.id+ '_thumbnail_container').css('visibility','visible');

        },                  


    function() {
    $('#' +this.id+ '_thumbnail_container').animate({"opacity" : 0}, 150, function() { });

    $('#' +this.id+ '_thumbnail_container').css('visibility','hidden');
});
Sign up to request clarification or add additional context in comments.

2 Comments

aha so (this) passes down all the time? Thank you!
this is always a reference to the DOM object (NOT the jQuery object).

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.