Skip to content

Instantly share code, notes, and snippets.

@mmarinero
Last active August 29, 2015 14:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mmarinero/42bddcf4396017ebd661 to your computer and use it in GitHub Desktop.
Jquery match plugin, works like .find() but throws errors on empty selectors or when the number of elements found doesn't match the second parameter (optional).
jQuery.fn.extend({
match: function(selector, matched) {
var result = this.find(selector);
if (matched === undefined && !result.length) {
throw new Error("Not matched elements for selector" + selector);
}
if (matched !== undefined && result.length !== matched) {
throw new Error("Found elements don't match expected ones: " +
"expected: " + matched +
", found: " + result.length +
", selector: '" + selector + "'"
);
}
//all tests passed
return result;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment