Skip to main content

Hi, I'm Mariano Guerra, below is my blog, if you want to learn more about me and what I do check a summary here: marianoguerra.github.io or find me on twitter @warianoguerra or Mastodon @marianoguerra@hachyderm.io

case insensitive contains in jquery 1.8 (:containsi)

say you want to do some filtering based on the text content of some nodes, in that case you would use :contains

but what if you want it to be case insensitive?

soon after searching you will find this jquery issue and copy and paste code from 3 years ago to realize that it breaks in jquery >= 1.8

then you will use this version:

function jqueryContainsI(text) {
    return function (elem) {
        return (elem.textContent || elem.innerText || $.text(elem)).toLowerCase().indexOf(text.toLowerCase()) > -1;
    };
}

jqueryContainsI.sizzleFilter = true;

// add case insensitive :contains filter called :containsi (see the last i)
$.extend($.expr[':'], {
    'containsi': jqueryContainsI
});