1

I'm looking for a solution to have a case-insensitive version of the :contains selector in jQuery. I found these two posts here on Stack Overflow:

Is there a case insensitive jQuery :contains selector?
How do I make jQuery Contains case insensitive, including jQuery 1.8+?

So basically the solution is this:

jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

My only question is how do I use this? Where to I specify this piece of code? I'm loading jQuery in my header and afterwards I load my script.js file where I have my DOM ready function declared. I put this piece of code outside the DOM ready function but it doesn't change anything in the behaviour of the :contains selector.

Community
  • 1
  • 1
matt
  • 42,713
  • 103
  • 264
  • 397

1 Answers1

3

If you put this piece of code after loading jquery, you will have one contains and one (case-insensitive) icontains selector:

$.extend($.expr[':'], {
        // case insensitive version of :contains
        icontains: function(a,i,m){
            return (a.textContent||a.innerText||jQuery(a).text()||"").
                toLowerCase().indexOf(m[3].toLowerCase())>=0;
        }
    });
jcubic
  • 61,973
  • 54
  • 229
  • 402
Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210