Raise an exception if jQuery query matches no elements
jQuery.fn.single = function() {
if (this.length != 1) {
throw new Error("Expected 1, got " +this.length);
}
return this;
};
var headers = $("h1").single(); //boom!
Written by Wojtek Kruszewski
Related protips
1 Response
I stole it from: http://stackoverflow.com/a/3719718/153786
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Dom
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#