www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
Methods
 getAttribute Method
getAttribute Method

Retrieves the value of the specified attribute.

Syntax

vAttrValue = object.getAttribute(sAttrName [, iFlags])

Parameters

sAttrName Required. String that specifies the name of the attribute.
iFlags Optional. Integer that specifies one or more of the following flags:
0Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.
1Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in sAttrName must exactly match those in the attribute name. If the iFlags parameter for getAttribute is set to 1 and this option is set to 0 (default), the specified property name might not be found.
2Returns the value exactly as it was set in script or in the source document.

Return Value

Variant that returns a String, number, or Boolean value as defined by the attribute. If the attribute is not present, this method returns null.

Remarks

If two or more attributes have the same name (differing only in uppercase and lowercase letters) and iFlags is 0, the getAttribute method retrieves values only for the last attribute created with this name, and ignores all other attributes with the same name.

When retrieving the CLASS attribute using this method, set the sAttrName to be "className", which is the corresponding Dynamic HTML (DHTML) property.

This method is used only by events created from HTML Components.

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, VAR, WBR, XMP

See Also

removeAttribute, setAttribute
Tags What's this?: Add a tag
Community Content
 
Add Community Content
Obscure bug in getAttribute()      John Sudds - MSFT   |   Edit   |  

Be aware that there is an obscure bug in this method when used on a form element containing an input element which has the name attribute set to "id".
Example:

<form id="myForm1">
<input id="user_id" name="user_id" value="text" />
</form>
<form id="myForm2">
<input id="id" name="id" value="text" />
</form>
<script type="text/javascript">
var formElement1 = document.getElementById('myForm1');
var formElement2 = document.getElementById('myForm2');
alert(formElement1.getAttribute('id')+ "\n" + formElement2.getAttribute('id'));
</script>

You would expect it to display a JavaScript alert box containing:
myForm1
myForm2

But instead you get:
myForm1
[object]

It appears that this method erroneously returns the input with the name "id" instead of the value of the id attribute of the form itself. This also happens if you use formElement2.id .

To work around the bug, use attributes['id'].value or getAttributeNode('id').value

See: http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes (jmaxwilson)

----

[jsudds.msft] By way of explanation, this behavior is due in part to the adding the name attribute as an expando property of the <form> object. In this case, the name attribute overwrites a well-known property (id) in the DOM. The same thing would happen to any of the other properties if a well-known name was used, such as <input name='className' />. The getAttribute method in IE searches for property values using expandos and COM properties rather than the attribute node, as does setAttribute(). This combination of factors can lead to unexpected results.

Tags What's this?: dhtml (x) Add a tag
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG