Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery end() function with Destructive methods

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.

like image 434
maximus Avatar asked Sep 17 '26 07:09

maximus


1 Answers

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.

like image 104
mVChr Avatar answered Sep 18 '26 20:09

mVChr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!