HTML DOM Element getAttribute()

Examples

Get the value of the class attribute of an element:

let text = element.getAttribute("class");
Try it Yourself »

Get the value of the target attribute of an <a> element:

let text = myAnchor.getAttribute("target");
Try it Yourself »

More examples below.


Description

The getAttribute() method returns the value of an element's attribute.


Syntax

element.getAttribute(name)

Parameters

Parameter Description
name Required.
The name of the attribute.


Return Value

Type Description
StringThe attribute's value.
null if the attribute does not exist.

More Examples

Get the value of the onclick attribute of a <button> element:

let text = myButton.getAttribute("onclick");
Try it Yourself »

Browser Support

element.getAttribute is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.