Why doesn't this work?
var spans = $(); var elem = document.getElementById('someId'); spans.add(elem);
What is the proper way to start off with an empty collection and add elements to it? I want to loop through a collection of ids and find the element on the page and add it to the matched set.
Quoting from the jQuery website:
Given a jQuery object that represents a set of DOM elements, the
.add()
method constructs a new jQuery object from the union of those elements and the ones passed into the method.
Hence, when you do .add()
it will not save the added elements, you would have to explicitly assign the element to the newly created object i.e
var $elements = $('.elements'); $elements = $elements.add($('#anotherelement'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With