Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  79] [ 1]  / answers: 1 / hits: 75551  / 10 Years ago, thu, june 5, 2014, 12:00:00

In the following example the stored jQuery selector returns the wrong value.
There is a possibility to store the selectors and not the result?



The js code:



// storing the jQuery selectors
var
$container = $( '.container' ),
$element1 = $container.find( '.element' ),
$element2 = $( '.element', $container ),
$element3 = $( '.element' );

// append elements to the container
for( i=0; i<10; ++i ){
$container.append( $(element_html) );
}

// try the stored selectors -> returns 0
console.log( 1: + $element1.length );
console.log( 2: + $element2.length );
console.log( 3: + $element3.length );


Why, if I use the container selectors to find the elements, it works?
It is beacuse the selector returns the pointer to the matched elements and not the elements?



// this works
console.log( 1: + $container.find( '.element' ).length );
console.log( 2: + $( '.element', $container ) .length );
console.log( 3: + $( '.element' ) .length );


jsFiddle demonstration


More From » jquery

 Answers
33

You have a fundamental misunderstanding of what



variableName = $(selector here);


does. It does not store the selector. It runs the selector you give against the current elements in the DOM, creates a jQuery object, puts the matching elements in the jQuery object, and gives you a reference to that jQuery object. The selector is not stored (modulo jQuery internals).



So given:



<body>
<div class=foo>x</div>
</body>


Then:



var $divs = $(div.foo);
console.log($divs.length); // 1


Gives us this:



$divs



If we then add another matching div:



$('<div class=foo></div>').appendTo(document.body);


Our $divs still only points to the first one; adding another matching element to the DOM had no effect on the jQuery object referenced from $divs.



$divs



If we re-run the query at that point:



$divs = $(div.foo);


...then we have:



$divs



If you have a jQuery object containing a DOM element, and you add descendant elements to that DOM element, then using that jQuery object with (say) .find will see the descendants. That's because the parent DOM element is already there in the jQuery object. E.g., adding a span to one of the divs that we already reference from our jQuery object:



$divs



If we were to use .find on $divs at that point looking for a span, we'd find it, because it's a descendant of one of the elements we already had a reference to.



If you want to re-run the DOM search again later to look for matching elements, you just use $() again; this is implicit in the above, but for clarity:



var $divs = $(div.foo);
console.log($divs.length); // 1
$('<div class=foo></div>').appendTo(document.body);
console.log($divs.length); // Still 1
$divs = $(div.foo);
console.log($divs.length); // Now it's 2


So storing a selector, when needed, is a matter of storing the selector string somewhere, not the jQuery object.


[#70713] Wednesday, June 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
levidaylonj

Total Points: 392
Total Questions: 100
Total Answers: 112

Location: Bahrain
Member since Fri, Sep 16, 2022
2 Years ago
;