setNamedItem
TOC
Collapse the table of content
Expand the table of content

setNamedItem method

Adds an attribute to an element by using an attributes collection.

Document Object Model (DOM) Level 3 Core Specification, Section 1.4

 

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.

Examples

The following example shows how to add an attribute to an element using the setNamedItem method.


<!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 &lt;div&gt; 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:
© 2017 Microsoft