01-10-2011

jQuery Check if an attribute exists with jQuery attr

To see whether an attribute on an html or xml element is actually defined / present, you can do the following:

if ($('#bla').attr('myattr') !== undefined) {
    // attribute exists
} else {
    // attribute does not exist
}
 

Comments:

Your comment:

»
Lenin 28/03/2012, 3:47 pm
For avoid problems with old versions of jQuery, I do that with:
if(typeof $('#bla').attr('myattr') != 'undefined') { ...

 

[x]