getAttributeNode() in javascript

getAttributeNode() method in javascript

getAttributeNode() method is used to get the value of the specified attributes from a given element.

If the there are no attributes present, it returns null.

Instead of getAttributeNode() usually getAttribute() method is used.

Example - Get attribute value of the given element

Try Yourself

Example Explanation

  • In the above example we have a button with attributes (id, onclick) and a Javascript function is declared for it (fun_name();).
  • When the button is clicked Javascript function is called.
  • In the Javascript function, first we are getting the element (document.getElementById("idBtn");) and storing in elem.
  • Next using the getAttributeNode method, we are retrieving all the attribute names present in the element and displaying it (alert(elem.getAttributeNode("onclick").value);).