HTML DOM setAttribute() Method Last Updated : 07 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The HTML DOM setAttribute() method is used to get the value of the attribute of the element. By specifying the name of the attribute, it can set the value of that element. If the element is already present then its value is updated. Syntax: Object.setAttribute(attributename,value) Parameters: This method accepts two parameters: attributename: It is a string whose value is to be set.value: It is a string value that is to be assigned to the attributename. Return Values: This parameter does not return any values. The below examples show the use of the setAttribute() method. Example 1: In this example, we will add a href attribute to the anchor tag using the setAttribute() method. HTML <!DOCTYPE html> <html> <body> <h1 style="color:green"> Geeks for Geeks </h1> <h2>setAttribute() Method</h2> <a id="geek">Click to go to geeksforgeeks.org</a> <p> Click the button below to add an href attribute to the element above. </p> <button onclick="myFunction()">Click Me</button> <script> function myFunction() { document.getElementById("geek").setAttribute ("href", "https://www.geeksforgeeks.org"); } </script> </body> </html> Output: HTML DOM setAttribute() Method Example 2: In this example, we will change the type of an input tag to a button using the setAttribute() method. HTML <!DOCTYPE html> <html> <body> <h1 style="color:green"> Geeks for Geeks </h1> <h2>setAttribute() Method</h2> <input id="geeks" value="Hello Geek"> <p> Click the button below to change the input field to an input button. </p> <button onclick="changeType()">Change</button> <script> function changeType() { document.getElementById("geeks").setAttribute("type", "button"); } </script> </body> </html> Output: HTML DOM setAttribute() MethodWe have a complete list of HTML DOM methods, to check those please go through this HTML DOM Object Complete reference article. Supported Browsers: The browser supported by DOM setAttribute() method are listed below: Google Chrome 1.0Internet Explorer 5.0Microsoft Edge 12.0Firefox 1.0Opera 8.0Safari 1.0 Comment More infoAdvertise with us Next Article HTML DOM setAttribute() Method A amitsingh2730 Follow Improve Article Tags : HTML Similar Reads HTML DOM setAttributeNode() Method 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. Syntax: element.setAttributeNode(name) Parameter: Only one parameter is accepted name.Where the name is the attribute node 1 min read HTML DOM removeAttribute() Method The DOM removeAttribute() method is used to remove an attribute with specified name from the element. It is similar to the removeAttributeNode() method but the difference is that the removeAttributeNode method is used to remove the specified attribute object, but on the other hand, removeAttribute r 1 min read HTML DOM removeAttributeNode() Method The DOM removeAttributeNode() method is used to remove the specified attribute from the current element. It is similar to removeAttribute() method but the difference is that the removeAttribute method is used to remove the attribute with the specified name, but on the other hand removeAttributeNode 1 min read HTML DOM setNamedItem() Method The setNamedItem() method is used to add a particular node to an attribute node using its name. These attribute nodes are collectively called as namedNodeMap. It can be accessed through a name. If a node is already present in the document, it will replace it and return the updated value. The setName 2 min read HTML DOM name Property The DOM name Property is used to return the name of the attribute. It is used for read-only. Syntax: attribute.name Return Value: This property returns a string value that represents the name of the attribute. Example: In this example, we are using DOM name Property for the attribute. HTML <!DOCT 1 min read Like