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

JqueryContainsCase SensitiveCase Insensitive

Jquery Problem Overview


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

https://stackoverflow.com/questions/187537/is-there-a-case-insensitive-jquery-contains-selector/187557#187557

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.

Jquery Solutions


Solution 1 - Jquery

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 - Jquery

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 - Jquery

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 - Jquery

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...

Solution 5 - Jquery

i'll allow myself to add my friends:

$.expr[":"].containsNoCase = function (el, i, m) { 
    var search = m[3]; 
    if (!search) return false; 
    return eval("/" + search + "/i").test($(el).text()); 
}; 

Solution 6 - Jquery

I was just able to ignore jQuery's case sensitivity altogether to achieve what I want using below code:

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

You can use this link to find code based on your jQuery versions to ignore case sensitivity, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

Also if you want to use :contains and make some search you may want to take a look at this: http://technarco.com/jquery/using-jquery-search-html-text-and-show-or-hide-accordingly

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatrymView Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JqueryEllipsisView Answer on Stackoverflow
Solution 3 - JqueryMina GabrielView Answer on Stackoverflow
Solution 4 - JqueryErickBestView Answer on Stackoverflow
Solution 5 - JquerybresleveloperView Answer on Stackoverflow
Solution 6 - JqueryUmesh PatilView Answer on Stackoverflow