Javascript Reference
Categories: Element

javascript Element setAttribute( )

@Feb. 1, 2010, 4:04 p.m.
setAttribute( ) Firefox/Netscape/NN 6 IE 4 Chrome/Safari/DOM 1  

setAttribute("attributeName", value)
setAttribute("attributeName", value[, caseSensitivity])


Sets the value of the named attribute within the current element. If the attribute is reflected in the object model as a property, this method acts the same as assigning a value to the object's property. Even so, the W3C DOM declares the setAttribute( ) method as the preferred way to adjust an attribute value (and the getAttribute( ) method for reading the value).

If the attribute does not yet exist in the element, the setAttribute( ) method adds the attribute as a name/value pair to the element (except in IE 4 through 5.5, the newly added attribute is not reported as part of the element's attributes collection).

IE treats the attribute names more as object property names. Therefore, when a discrepancy exists between the attribute and corresponding property names (e.g., class versus className), IE requires the property name version. To assign a new value to the class attribute of an element for both IE and Navigator, you should branch the code to invoke the method only once per browser to avoid adding an unused className attribute to the Navigator element. For purposes of object detection, a browser that supports the W3C DOM approach returns a string value type for the element's getAttribute("class") method.

Values you assign to an attribute must be all strings for Netscape 6 (the W3C DOM specification). IE allows other data types (such as Number and Boolean), but if you assign, say, a numeric value in string form, the data type gets converted so that getAttribute( ) returns the value in IE's preferred data type. In Netscape 6, all attribute values are strings.

Attribute names in Netscape 6 are not case-sensitive, but you should get in the habit of using all lowercase attribute names (in the direction of XHTML). IE is case-sensitive about attribute names for this method by default. An optional third parameter lets you control whether the attribute name should be treated in a case-sensitive manner. Avoid playing case-sensitivity tricks with attribute names (two different attributes with the same spelling but different case characteristics). If you use all lowercase attribute names for all your code, you can omit the third IE parameter while staying W3C DOM compliant.

  • The attribute name used in the HTML tag (except as noted above for IE).
  • For Netscape 6, the attribute value as a string data type. For IE, the attribute value as a string, number, or Boolean, as dictated by the attribute's data type. Strings are safe for all values, although IE internally converts the data types as necessary.
  • An optional integer value for IE only. If 1 (the default), the attribute in the HTML tag must match the case of the attributeName parameter exactly for its value to be set (allowing for multiple attribute names with the same spelling but different cases to coexist). If 0, the attributeName parameter aligns itself with the first attribute with the same name, regardless of case.
Returned Value


Under moderation.
Shawnneind - 3 months
Under moderation.
LarryLom - 4 months
Under moderation.
LarryLom - 4 months

Powered by Linode.