In this section, we will learn what the Element getAttributeNode() method is and how to use it in JavaScript.
What is Element getAttributeNode() Method in JavaScript?
The JavaScript Element getAttributeNode() method takes the name of an attribute and will return an Attribute object that represents the declared attribute.
Here are some of the important properties of this object:
– attr.isId: using this property we can see if the attribute is an id or not. Basically, we can see if the argument of the getAttributeNode() method was “id” or not. It returns the value true if it is “id” attribute and false otherwise.
– attr.name: this property returns the name of the attribute (the value that was set as the argument of the method).
– attr.value: this property returns the value of the specified attribute. (Note, if the attribute does not have a value, the return value might be an empty string or null).
– attr.specified: this property specifies if the target attribute is declared and has a value associated with it or not.
Element getAttributeNode() Method Syntax:
element.getAttributeNode(“attribute-name”);
Element getAttributeNode() Method Parameter
The method takes one argument and that is the name of the attribute we want to get an Attribute object from it.
Element getAttributeNode() Method Return Value
The return value of this method is an Attribute object that has methods and properties that allow us to work with the target attribute.
For example, using this object, we can get the name and value of the specified attribute.
Example: using getAttributeNode() method in JavaScript
See the Pen using getAttributeNode() method in JavaScript by Omid Dehghan (@odchan1) on CodePen.