Methods


setAttribute Method

Sets the value of the specified attribute.

Syntax

object.setAttribute(sAttrName, vValue [, iCaseSensitive])

Parameters

sAttrName Required. String that specifies the name of the attribute.
vValue Required. Variant that specifies the string, number, or Boolean to assign to the attribute.
iCaseSensitive Optional. Integer that specifies whether to use a case-sensitive search to locate the attribute. Can be one of the following values:
1
The case of sAttrName is respected.
0
Match sAttrName regardless of case.

Return Value

No return value.

Remarks

 New for Windows Internet Explorer 8 Internet Explorer 8 and later. IE8 mode enables several enhancements to the setAttribute, getAttribute, and removeAttribute methods that are not available when pages are displayed in earlier document modes.

  • The sAttrName parameter requires the name of the desired content attribute and not the Document Object Model (DOM) attribute. For example, in IE8 mode, this method no longer requires sAttrName to be "className" when setting, getting, or removing a CLASS attribute. Earlier versions of Internet Explorer and Internet Explorer 8 in compatibility mode still require sAttrName to specify the corresponding DOM property name.
  • The sAttrName parameter is not case sensitive. As a result, the iCaseSensitive parameter is no longer supported and should not be used.
  • The methods support event handlers. For example, the following code example defines an event handler to call a function called SomeFunction when the body of the page is loaded.
    document.body.setAttribute('onload', 'SomeFunction()');

If the specified attribute is not already present, the setAttribute method adds the attribute to the object and sets the value.

If your pages are displayed in IE5 mode or IE7 mode, be careful when spelling attribute names. If you set iCaseSensitive to 1 and the sAttrName parameter does not have the same uppercase and lowercase letters as the attribute, a new attribute is created for the object. If two or more attributes have the same name, differing only in case, and iCaseSensitive is set to 0, this method assigns values only to the first attribute created with this name. All other attributes of the same name are ignored.

 New for Internet Explorer 8 Internet Explorer 8 and later. When pages are displayed in IE8 mode, the vValue parameter only supports string values. Non-string values are not automatically converted to string values. For best results, formally convert values to strings before using them as parameter values. For example, do not attempt to pass an object directly to the vValue parameter; call the object's toString method instead.

Example

Click on the following Show Me button to see example usage of the setAttribute method.

This feature requires Microsoft Internet Explorer 7.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 World Wide Web link.

Applies To

A, ADDRESS, APPLET, AREA, B, BASE, BASEFONT, BGSOUND, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, currentStyle, CUSTOM, DD, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, FRAME, FRAMESET, HEAD, hn, HR, HTML, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, KBD, LABEL, LEGEND, LI, LINK, LISTING, MAP, MARQUEE, MENU, META, nextID, NOBR, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, runtimeStyle, S, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, STRONG, style, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TITLE, TR, TT, U, UL, userProfile, VAR, WBR, XMP, CSSRuleStyleDeclaration Constructor, CSSStyleDeclaration Constructor, Element Constructor, Event Constructor

See Also

Tags :


Page view tracker