Case-insensitive attribute matching

By PennyBreed on Mar 12, 2014

This custom selector will match element attribute values, with case-insensitivity.

$.expr[':'].attrNoCase = function(node, stackIndex, properties){
    var args = properties[3].split(',').map(function(arg) {
        return arg.replace(/^\s*["']|["']\s*$/g, '');  
    });
    if ($(node).attr(args[0])) {
        return $(node).attr(args[0]).toLowerCase() == args[1].toLowerCase();
    }
};

//Example Usage
$('div:attrNoCase(someAttribute,some value)');
$('div:attrNoCase("someAttribute","some value")');

Comments

Sign in to comment.
Hawkee   -  Mar 13, 2014

I didn't realize these were case sensitive in the first place. I guess I never capitalize my element attributes.

PennyBreed  -  Mar 13, 2014

I don't purposely capitalize mine either, but when matching attribute values that are input by the user, this is useful.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.