Skip to content
Related Articles

Related Articles

Improve Article

HTML | DOM setAttributeNode() Method

  • Last Updated : 26 Jul, 2019

The setAttributeNode() method in HTML DOM is used to add the specified attribute node to an element. If the specified attribute is already present, then this method replaces it.



Parameter: Onll one parameter is accepted name.Where name is the attribute node to be added. It is the required field.

Return Value: This method returns an attribute object which represents the replaced attribute node otherwise it returns null.


<!DOCTYPE html>
      HTML DOM setAttributeNode Method
        .gfg {
            color: green;
<body style="text-align: center;">
    <h1 style="color:green;">
       DOM setAttributeNode Method
    <p id="p">
        A computer science portal for geeks.
    <button onclick="Geeks()">
        Click Here!
        function Geeks() {
            //Get the paragraph to add attribute. 
            var doc = document.getElementById("p");
            //Creating a class attribute.
            var attr = document.createAttribute("class");
            //Setting the value of class attribute.
            attr.value = "gfg";
            //Adding class attribute to paragraph. 


Before click on the button:

After click on the button:

Supported Browsers: The browser supported by setAttributeNode() method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :