I don’t understand why the following code doesn’t work. (jQuery 1.6.4)
HTML:
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test me"></div>
<div class="test me"></div>
<div class="test me"></div>
JS:
var rows = $([]);
console.debug('Divs we want: ', $('div:not(.me)').length);
console.debug('Rows before: ', rows.length);
$('div:not(.me)').each(function() {
if (1==1) { // some conditional
console.debug($(this));
rows.add($(this));
}
});
console.debug('Rows after: ', rows.length);
add() builds and returns a new jQuery object from the contents of the one it’s called on and the argument you supply. It does not update the object it’s called on.
You probably want to write:
As an aside, since jQuery 1.4 the canonical way to construct an empty jQuery object is to call
$()without arguments: