Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive jQuery attribute selector

People also ask

Is jQuery ID selector case sensitive?

jQuery attribute value selectors are generally case-sensitive.

How use contains in jQuery?

jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).


The simplest way to do this is to add a case insensitivity flag 'i' inside the regex part of the selector:

So instead of

$( "input[name*='man']")

You could do

$( "input[name*='man' i]")

JS fiddle: https://jsfiddle.net/uoxvwxd1/3/


You can always use .filter():

var mans = $('input').filter(function() {
    return $(this).attr('name').toLowerCase().indexOf('man') > -1;
});

mans.css('background-color', 'black');

The key part here is toLowerCase() which lowercases the name attribute, allowing you to test it for containing man.


var control = $('input').filter(function() {
    return /*REGEX_VALUE*/i.test($(this).attr('id'));
});

*REGEX_VALUE* - the value you want to find

I ended up using regex to validate whether the attribute 'ID' satisfy... regex is much more flexible if you want to find a certain matching value or values, case sensitive or insensitive or a certain range of values...


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, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

Also there is an article where it does to many good things with jQuery: http://www.ultechspot.com/jquery/using-jquery-search-html-text-and-show-or-hide-accordingly