getAttributeNames() in javascript

getAttributeNames() method in javascript

getAttributeNames() method is used to get all the names of the attributes specified in an given element.

If there are no attributes present, it returns null.

Example - Get attribute names 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 getAttributeNames method, we are retrieving all the attribute names present in the element and displaying it (alert(elem1.getAttributeNames());).