I'm having some problems grokking the .end() function in jQuery. The docs I've read advertise it as "rolling back" changes from a filter or additional selection. For example,
var someDivs = $('div');
someDivs
.filter('li')
.css('color', 'green')
.end(); // ok, reverted back to original wrapper
On the other hand, this doesn't work.
someDivs
.get(0) // get DOM element at index 0
.end(); // error, executing a jQuery method on a plain javascript object.
I'm just trying to verify that I'm correct that after certain chain-destructive methods like html(), text(), and get() are called, it's impossible to revert back to the original wrapper.
Thanks in advance.
Yes, end() will only work on methods that return a jQuery object. Methods that return strings (.html(), .text()) or DOM elements (.get()) won't allow you to chain any jQuery methods at all subsequently.
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