gshaffer gshaffer - 2 months ago 6x
Javascript Question

Case insensitive jQuery attribute selector

I am doing the following using attribute contains selector $('[attribute*=value]')

<input name="man-news">
<input name="milkMan">

$( "input[name*='man']").css("background-color:black");

This works for the 1st input but not the second input as "Man" has a capital "M"

How can I make
$( "input[name*='man']")
an case insensitive selector?


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.