Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created October 8, 2012 15:38
Show Gist options
  • Save shaneriley/3853159 to your computer and use it in GitHub Desktop.
Save shaneriley/3853159 to your computer and use it in GitHub Desktop.
jQuery Exact Contains
// Problem: jQuery's :contains expression does not do an exact match.
// Solution: Exact contains `$("div").filter(":econtains(smart)");`
$.extend($.expr[":"], {
econtains: function(obj, index, meta, stack) {
return (obj.textContent || obj.innerText || $(obj).text() || "").toLowerCase() == meta[3].toLowerCase();
}
});
$("div").filter(":contains(smart)").length === 1 // false
$("div").filter(":econtains(smart)").length === 1 // true
<div>smart</div>
<div>smart-ass</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment