Skip to main content

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
});

Comments

Comments powered by Disqus