Skip to content

Instantly share code, notes, and snippets.

@mmarinero mmarinero/jQuery.match.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.