HTML DOM setNamedItem() Method Last Updated : 09 Jun, 2023 Comments Improve Suggest changes Like Article Like Report 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 setNamedItem() method requires a node as a parameter. Syntax: namedNodeMap.setNamedItem( node ) Parameter Value: This method contains a single parameter node which is mandatory. This parameter is the node value that need to add or replace in the namedNodeMap collection. Return Value: It returns a Node object that, represents the replaced node (if any), otherwise null Example 1: In this example, we will use the setNamedItem() method. HTML <!DOCTYPE html> <html> <head> <title> DOM setNamedItem() Method </title> <style> .gfg { color: green; font-size: 40px; } </style> </head> <body> <h1 class="geeks1">GeeksforGeeks</h1> <h2 class="geeks2">DOM setNamedItem() Method</h2> <button onclick="setNameItem()"> Set Node </button> <script> function setNameItem() { // It is used to fetch the text // present in class geeks1 let one = document.getElementsByClassName("geeks1")[0]; // New class (geek) is created let geek = document.createAttribute("class"); // Passing value of gfg class into geek class geek.value = "gfg"; // Updating the CSS property of geeks1 // class to gfg class one.attributes.setNamedItem(geek); } </script> </body> </html> Output: Example 2: In this example, we will use the setNamedItem() method. HTML <!DOCTYPE html> <html> <head> <title> DOM setNamedItem() Method </title> <!-- Set CSS property to the element --> <style> .gfg { color: green; font-size: 40px; } .gfg1 { color: green; font-size: 40px; } </style> </head> <body style="text-align:center"> <h1>GeeksforGeeks</h1> <h2>DOM setNamedItem() Method</h2> <h3>Welcome to GeeksforGeeks</h3> <button onclick="setNameItem()"> Set Node </button> <script> function setNameItem() { let one = document.getElementsByTagName("H1")[0]; let geek = document.createAttribute("class"); geek.value = "gfg"; one.attributes.setNamedItem(geek); let two = document.getElementsByTagName("H3")[0]; let geek1 = document.createAttribute("class"); geek1.value = "gfg1"; two.attributes.setNamedItem(geek1); } </script> </body> </html> Output: Supported Browsers: The browser supported by DOM setNamedItem() Method are listed below: Google Chrome 1Edge 12Internet Explorer 6Firefox 1Opera 12.1Safari 1 Comment More infoAdvertise with us Next Article HTML DOM setNamedItem() Method A Abhishek7 Follow Improve Article Tags : Web Technologies HTML Web technologies HTML-DOM Similar Reads HTML DOM innerHTML Property The innerHTML property sets or returns the HTML content (inner HTML) of an element. It allows you to manipulate the inner content, including HTML tags, by assigning new HTML strings or retrieving existing content, making it useful for dynamic updates on web pages.SyntaxIt returns the innerHTML Prope 2 min read HTML DOM firstChild Property The DOM firstChild Property is used to return the firstchild Node of its parent node element. It is read-only property and may return an element node, a text node or a comment node. Syntax:node.firstChild Return Value: It returns a string value which represent the first child of a node. If the eleme 2 min read HTML DOM parentNode Property The parentNode property is used to return the parent node of the specified node as a Node object. It is a read-only property. Syntax: node.parentNode Return value: This property returns a parent element of the specified node or null if the current node has no parent element. Example: In this example 2 min read HTML | DOM Abbreviation Object The DOM Abbreviation Object is used to represent the HTML <abbr> element. The abbreviation element is accessed by getElementById(). Example 1: html <!DOCTYPE html> <html> <head> <title> HTML DOM Abbreviation Object </title> <script> function myGeeks() { var 1 min read HTML | DOM Style margin Property The DOM Style margin Property is used to sets or returns the margin of an element.We can set the different size of margins for individual sides(top, right, bottom, left).Margin properties can have following values: Length in cm, px, pt, etc.Width % of the element.Margin calculated by the browser: au 2 min read HTML DOM nodeName Property The nodeName property is used to return the name of the specified node as a string. It returns different values for different nodes such as if the node attributes, then the returned string is the attribute name, or if the node is an element, then the returned string is the tag name. It is a read-onl 2 min read HTML DOM textContent Property The textContent property in HTML is used to set or return the text content of the specified node and all its descendants. This property is very similar to nodeValue property but this property returns the text of all child nodes.Syntax: It is used to set the text of node. node.textContent = textIt is 2 min read HTML ondrop Event Attribute The ondrop event attribute is used to drag an element or text and drop it into a valid droppable location or target. The drag and drop is a common feature of HTML 5. There are different events that are used and occur before ondrop event. Events occur on the draggable targetEvents occur on the drop t 3 min read HTML DOM item() Method The item() method is used to return the node at the specified index. The nodes are sorted as they appear in the source code. The index of the node list starts with 0. Syntax:nodelist.item( index )ornodelist[ index ] Parameters: This method accepts single parameter index which is used to hold the ind 2 min read HTML DOM Style borderRight Property The DOM style borderRight property is used to set or return the three different border-right properties such as border-right-width, border-right-style, and border-right-color of an element. Syntax: It returns the borderRight Property.object.style.borderRightIt is used to set the borderRight property 2 min read Like