mozilla

Document.createAttribute()

createAttribute creates a new attribute node, and returns it.

Syntax

attribute = document.createAttribute(name) 

Parameters

  • attribute is an attribute node.
  • name is a string containing the name of the attribute.

Example

<html>

<head>
<title> create/set/get Attribute example</title>

<script type="text/javascript">

function doAttrib() {
  var node = document.getElementById("div1");
  var a = document.createAttribute("my_attrib");
  a.value = "newVal";
  node.setAttributeNode(a);
  alert(node.getAttribute("my_attrib")); // "newVal"
}

// alternative form not actually using createAttribute
//function doAttrib() {
//  var node = document.getElementById("div1");
//  node.setAttribute("my_attrib", "newVal");
//  alert(node.getAttribute("my_attrib")); // "newVal"
//}

</script>
</head>

<body onload="doAttrib();">
<div id="div1">
<p>Some content here</p>
</div>
</body>
</html>

Notes

The return value is a node of type attribute. Once you have this node you can, as in the foregoing example, set its value by assigning a string to the nodeValue property, or in the alternate form by using the setAttribute() method. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.

Specification

See also

Document Tags and Contributors

Last updated by: teoli,
Hide Sidebar