Possible Duplicate:
Check existence of an attribute with JQuery
How do you check if there is an attribute on an element in jQuery? Similar to hasClass
, but with attr
?
For example,
if ($(this).hasAttr("name")) {
// ...
}
Possible Duplicate:
Check existence of an attribute with JQuery
How do you check if there is an attribute on an element in jQuery? Similar to hasClass
, but with attr
?
For example,
if ($(this).hasAttr("name")) {
// ...
}
Late to the party, but... why not just
this.hasAttribute("name")
?Refer This
You're so close it's crazy.
There's no hasAttr but hitting an attribute by name will just return undefined if it doesn't exist.
This is why the below works. If you remove the name attribute from #heading the second alert will fire.
Update: As per the comments, the below will ONLY work if the attribute is present AND is set to something not if the attribute is there but empty
The best way to do this would be with
filter()
:It would still be nice to have .hasAttr(), but as it doesn't exist there is this way.