hasAttribute (W3C DOM Core method)
| Version | Depr. | Static |
|---|---|---|
| DOM2 | No | No |
| IE7 | FF1.5+ | SA1.3+ | OP9.5+ |
|---|---|---|---|
| None | Buggy | Buggy | Buggy |
- Returns
Boolean
Example
var hastype = element.hasAttribute('type');
The
example above tests whether an element has a type
attribute, and saves the result to the hastype
variable.
So if the element in question were this HTML:
<link rel="stylesheet" type="text/css" href="default.css" />
Then
the hastype variable would have the boolean value
true. However if the element were this HTML:
<link rel="contents" href="/sitemap" title="Sitemap" />
Then
the hastype variable would have the boolean value
false.
Description
Whether this element has an attribute with the specified name, or has a specific default value for that attribute in this document type.
Return value
Returns
true if an attribute with the specified name is defined
for this element, or has a specific default value in this document type,
otherwise false
Compatibility
| Internet Explorer | Firefox | Safari | Opera | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 1.5 | 2.0 | 3.0 | 1.3 | 2.0 | 3.0 | 9.0 | 9.5 |
| None | None | None | Buggy | Buggy | Buggy | Buggy | Buggy | Buggy | Full | Buggy |
Internet Explorer doesn't implement this method (it returns undefined).
Firefox,
Safari and Opera 9.5
do not match attributes which are not explicitly defined but have a
specific default value in this document type, such as the
shape attribute of an a element
(only Opera 9.0 gets this right).
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.




