How do I make jQuery Contains case insensitive, including jQuery 1.8+?

54,527

Solution 1

This is what i'm using in a current project, haven't had any problems. See if you have better luck with this format:

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

In jQuery 1.8 the API for this changed, the jQuery 1.8+ version of this would be:

jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {
    return function( elem ) {
        return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});

You can test it out here. For more detail on 1.8+ custom selectors, check out the Sizzle wiki here.

Solution 2

It's worth noting that the answer is correct but only covers :Contains, and not the alias :contains which could lead to unexpected behavior (or could be used by design for advanced applications that require both sensitive and insensitive search).

This could be resolved by duplicating the extention for the alias:

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

Took me a while to work out why it wasn't working for me.

Solution 3

I would do something like this

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

And Leave :contains Alone...

DEMO

So why jQuery doesn't support it in it's library?! if it is that easy...

because Does your code pass the turkey code?

Solution 4

May be late.... but,

I'd prefer to go this way..

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

This way, you DO NOT tamper with jQuery's NATIVE '.contains'... You may need the default one later...if tampered with, you might find yourself back to stackOverFlow...

Share:
54,527
Matrym
Author by

Matrym

Updated on June 15, 2020

Comments

  • Matrym
    Matrym almost 4 years

    I'm trying to use "contains" case insensitively. I tried using the solution at the following stackoverflow question, but it didn't work:

    Is there a case insensitive jQuery :contains selector?

    For convenience, the solution is copied here:

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

    Here is the error:

    Error: q is not a function
    Source File: /js/jquery-1.4.js?ver=1.4
    Line: 81
    

    Here's where I'm using it:

      $('input.preset').keyup(function() {
        $(this).next().find("li").removeClass("bold");
        var theMatch = $(this).val();
        if (theMatch.length > 1){
          theMatch = "li:Contains('" + theMatch + "')";
          $(this).next().find(theMatch).addClass("bold");
        }
      });
    

    My use of the original case sensitive "contains" in the same scenario works without any errors. Does anyone have any ideas? I'd appreciate it.

  • Matrym
    Matrym over 14 years
    You are a life saver. Thanks.
  • Ellipsis
    Ellipsis about 13 years
    I would have commented this on above, but apparently I'm not able to.
  • Justin Force
    Justin Force almost 13 years
    I'm not sure what you're getting at here. This overrides the expected, documented behavior of :contains. I think it's clearer to leave the original :contains alone and call your new selector :icontains.
  • Ellipsis
    Ellipsis over 12 years
    That's beside the point of my post. Creating an alternative selector is certainly a valid option, however I was pointing out that extending :contains, but neglecting to extend :Contains could lead to user confusion, when they yield different results.
  • Justin Force
    Justin Force over 12 years
    Then thank you for sharing this "gotcha." :) Still, I think it's a good idea to mention that it's better not to override the built-in behavior when it's less work to build a non-destructive, parallel behavior.
  • Abe Miessler
    Abe Miessler over 12 years
    Any chance you could talk about exactly what this is doing?
  • Nick Craver
    Nick Craver almost 12 years
    @ClovisSix - thanks for the heads up, I provided a 1.8+ method of doing the same, let me know if you have any trouble.
  • ObjectType
    ObjectType almost 12 years
    Thanks for the 1.8 change. Had a similar extension that stopped working with with the 1.8 release. Saved my butt.
  • albanx
    albanx over 10 years
    To Note that this does not replace :contains just add another selector :Contains.
  • Sebastian
    Sebastian about 10 years
    the selector should be called icontains, or something similar.
  • taylor michels
    taylor michels about 10 years
    I tried a few different solutions posted for this and this one finally worked. thanks.
  • outofmind
    outofmind over 8 years
    I'd suggest that it should be return arg != undefined && jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0; to prevent javascript error in case contains is called with empty arg