removeAttribute (W3C DOM Core method)
| Version | Depr. | Static |
|---|---|---|
| DOM1 | No | No |
| IE5.5+ | FF1.5+ | SA1.3+ | OP9.5+ |
|---|---|---|---|
| Buggy | Buggy | Buggy | Buggy |
- Returns
void- Throws
NO_MODIFICATION_ALLOWED_ERR
Example
element.removeAttribute('href');
The
example above removes the href attribute from an
element.
So if the element in that example referred to this HTML:
<a href="http://www.brothercake.com/" rel="me">brothercake</a>
Then the operation would result in this:
<a rel="me">brothercake</a>
Description
Remove an attribute with the specified name.
If the attribute has a specific default value in
this document type, removing the attribute should replace it with the
default value (as well as its corresponding namespaceURI, localName
and prefix if applicable).
This method
removes non-namespaced attributes; to remove a namespaced attribute, use
the DOM 2 removeAttributeNS method instead.
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 |
| Buggy | Buggy | Buggy | Buggy | Buggy | Buggy | Buggy | Buggy | Buggy | Full | Buggy |
In
Internet Explorer,
Firefox, Safari and
Opera 9.5 when removing an attribute which has
a specific default value in this document type (such as the
shape attribute of an <a>
element) the attribute is not replaced with the default (only
Opera 9.0 gets this right).
In
Internet Explorer in HTML, as with getAttribute, this method is mapped to
properties, not attributes; so for some attributes you need to use the
property name, such as className for a
class attribute and
htmlFor for a for
attribute (see getAttribute for more
details).
Additionally, in Internet Explorer in
HTML using this method on a style
attribute doesn't actually remove it; IE considers the value of a
style attribute to be a
style object, not a string, and using removeAttribute has the effect of resetting all
the style properties to their default values, while retaining the object
itself. Using this method on an attribute which IE considers to have a
boolean true value (such as
disabled, when defined) resets it to
false, and using it on an attribute IE considers to be
a number (such as tabindex) or a function
(event-handling attributes such as onclick) has no
effect at all.
Internet Explorer
implements a second argument to removeAttribute, which is a case-sensitivity flag
that can take the value 0 (case-insensitive) or
1 (default, case-sensitive). This argument works as
expected in IE, and does not affect any other browser.
Internet Explorer also implements a
return value, which should be boolean true or
false according to whether the attribute was
successfully removed. However in practise, it returns
true whenever an attribute should have been
removed, even if it actually wasn't (because of one of the value type
caveats mentioned above).
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.