YouTip LogoYouTip

Met Element Getattribute

# HTML DOM getAttribute() Method ## Definition and Usage getAttribute() method returns the attribute value of the specified attribute name. **Note:** If the element does not have the specified attribute, returns null or "". ## Syntax ```javascript element.getAttribute(attributename) ``` ## Parameters | Parameter | Type | Description | |---|---|---| | attributename | String | Required. The attribute name whose value you need to get. | ## Return Value | Type | Description | |---|---| | String | The attribute value, returns null if the attribute does not exist. | ## Browser Support | Element | |---| | Yes | Yes | Yes | Yes | Yes | ## Technical Details | DOM Version | |---| | Core Level 1 Element | ## More Examples ### Example 1 The following example returns the value of the 'class' attribute of the specified element: ```html

Get Attribute Value:

Smiley face

var x = document.getElementById("myImg").getAttribute("alt"); document.getElementById("demo").innerHTML = x; ``` ### Example 2 The following example shows how to return the names of all attributes of an element: ```html

function myFunction() { var x = document.getElementsByTagName("button"); var y = x.getAttributeNames(); var z = y.join(", "); document.getElementById("demo").innerHTML = z; } ``` ## Related Pages HTML Tutorial: ( JavaScript Tutorial: ( * Is the above content helpful to you: * * * ## Category Navigation * ( * ( * ( * (http://www.miibeian.gov.cn/)
← Met Element GetattributenodeProp Node Firstchild β†’