I have a custom plugin very simple. If it returns this after calling it, .end() works great. However if it returns $(this), .end() doesn’t work. Why does that happen? Am I missing something here?
Code:
$.fn.fnBar = function() {
$(this).html("hello!");
//return $(this); // Doesn't work
return this; // Works!
};
$("div")
.find("span")
.fnBar()
.end()
.css("color", "red");
From the documentation :
In
endsets the state of the filtering to the one before the call tofind.With just
$(this), you’re rebuilding a new jQuery object, without any history. You have no filtering operation and no chain, so it has nothing to cancel.jQuery
endfunction is implemented like this :This means you can see the difference by logging the
prevObjectproperty of your jQuery object :