getAttribute() in javascript

getAttribute() method in javascript

getAttribute() method is used to get the value of the specified attribute in an given element.

If the specified attribute is not present, it returns null.

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 getAttribute method, we are retrieving its attribute value and displaying it.