Skip to Content

Blog

You case-insensitive bastard

By: Michael Diedrick on Aug 12, 2014

Tags: Process (10) Tech Simplicity (6) How-Tos (2)

JQuery is an immensely useful framework in Javascript, and even better, when it misses something, like how the ':contains' selector only finds things that are the same case, it's surprisingly easy to extend.  

$.extend($.expr[":"], {
"containsNoCase": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});

Then change your :contains selector to :containsNoCase, like $("body:containsNoCase").each(....  And voila, you’ll shed a tear for both the little and big letters in no time.