DOM:document.createAttribute
From MDC
Contents |
[edit] Summary
createAttribute creates a new attribute node, and returns it.
[edit] Syntax
attribute = document.createAttribute(name)
[edit] Parameters
-
attributeis an attribute node. -
nameis a string containing the name of the attribute.
[edit] 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.nodeValue = "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>
[edit] 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.