Cached Jquery selector behavior that I do not understand
Posted
by gaoshan88
on Stack Overflow
See other posts from Stack Overflow
or by gaoshan88
Published on 2010-03-30T17:02:37Z
Indexed on
2010/03/30
17:13 UTC
Read the original article
Hit count: 232
jQuery
|JavaScript
Given the following code why am I getting different values for a
and b
? I would have thought they would return the same thing:
(function() {
var a = $('#foo');
var Test = function(){
console.log(a); //outputs 'jQuery()'
var b = $('#foo');
console.log(b); //outputs 'jQuery(select#foo)' which is what I want
};
})();
This question stems from me trying to stick frequently used selectors into vars. Originally I was doing it in each method (like I did with var b
in the above example) but then I found I needed to use the selectors in multiple methods so I moved the assignment out to be available (or so I thought) to all of the methods in that anonymous function. As you can see, it does not work. Why is this?
© Stack Overflow or respective owner