1

Here's my code:

   $('#details').hover(function() {
    var tag = 'div.cds'
    var offset = $(this).position();
    var width = $(tag).outerWidth();
    var height = $(tag).outerHeight();
    $(tag).show();
    $(tag).css('left', offset.left - width + 'px');
    $(tag).css('top', offset.top - height + 'px');
}, function() {
    $(tag).hide();
});

When I "mouseout", the variable "tag" does not hide.

JSFiddle here: http://jsfiddle.net/79kLc/

Thanks!

2 Answers 2

5

Learn about scope

var tag = 'div.cds'
 $('#details').hover(function() {
    var offset = $(this).position();
    var width = $(tag).outerWidth();
    var height = $(tag).outerHeight();
    $(tag).show();
    $(tag).css('left', offset.left - width + 'px');
    $(tag).css('top', offset.top - height + 'px');
}, function() {
    $(tag).hide();
});

The tag variable did not exist in the second function's scope. So i added tag to the global scope. and it should work now.

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

2 Comments

@milimetric, i was not tring to be mean :-P
Both these answers are correct. @Naveed's solution works best for my needs since I re-use the function multiple times and do not need to declare global vars for each function. But @Neal replied first so I'm accepting that one. Thanks to everyone however!
0

Because tag can't be referred in this scope. You'll have to use the selector again. Like this: $('div.cds').hide();

Update fiddle: http://jsfiddle.net/79kLc/1/

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.