Skip to content

Instantly share code, notes, and snippets.

@jdaly13
Last active January 18, 2018 20:43
Show Gist options
  • Save jdaly13/5581538 to your computer and use it in GitHub Desktop.
Save jdaly13/5581538 to your computer and use it in GitHub Desktop.
update jquery end method to include parameters
// with this you can write .end(3) instead of .end().end().end()
(function(){
// Define overriding method.
jQuery.fn.end = function(no_of_times){
var prevObject = this.prevObject;
if (!(arguments.length) || (typeof no_of_times !== "number")) {
return this.prevObject || this.constructor(null);
} else {
for(i = 1; i<no_of_times; i++) {
prevObject = prevObject.prevObject;
if(!prevObject) {
prevObject = this.constructor(null);
break
}
}
return prevObject;
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment