Blog

Welcome to the Byte Blog where we share our thoughts on design, technology, and occasionally, cats.

You case-insensitive bastard

by Michael Diedrick on Aug 12, 2014

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.


Add a Comment