setNamedItem method
Adds an attribute to an element by using an attributes collection.
![]() |
Syntax
var retval = attributes.setNamedItem(ppNode);Parameters
- ppNode [in]
-
Type: IHTMLDOMAttribute
An attribute object.
Return value
Type: IHTMLDOMAttribute
Returns the previously existing attribute if it has the same name as the new attribute, otherwise null.Standards information
Remarks
An attribute that is set with this method does not have to apply to the element.
If an attribute with the same name is already present, it is replaced by the new attribute.
setNamedItem was introduced in Microsoft Internet Explorer 6.
Examples
The following example shows how to add an attribute to an element using the setNamedItem method.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/setNamedItemEx1.htm
<!DOCTYPE html> <html> <head> <title>setNamedItem Method Sample</title> </head> <body onload="fnSetNamedItem();"> <h1>setNamedItem Method Sample</h1> <p>The <strong>setNamedItem</strong> method applies a ToolTip to the <strong>div</strong> element below.</p> <div style="background-color: #96F; color: #FFF; border-style: outset; border-width: 4px; border-color: #FF6; width: 200px;" id="myDIV"> This <div> now has a tooltip. </div> <script> function fnSetNamedItem() { var nnm = document.getElementById('myDIV').attributes; var namedItem = document.createAttribute("title"); namedItem.value = "This is a ToolTip"; nnm.setNamedItem(namedItem); } </script> </body> </html>
See also
- attributes
- Reference
- getNamedItem
- removeNamedItem
Show:
