How to check if an attribute exists on your jQuery Selection

I recently purchased the new jQuery in Action (2nd Edition) book and I found something that I didn’t know existed (or at least that you could use ‘is’ in this way)! I recently ran into the situation where I wanted to select a group of elements let’s say they were elements, and I wanted to perform an operation on those elements that had a certain custom attribute set. Below is the code to how you could find out:

$('span').each(function(){
    if($(this).is('[attribute_name])){
        alert("This one has that attribute!")
    }
});

You of course replace attribute name with the attribute you are searching for (id,name,class,fooBizzle, etc.)

Enjoy!

Saturday, July 3rd, 2010 jQuery

Leave a Reply

You must be logged in to post a comment.

[ad code=1 align=center]