We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

document.createAttribute Redirect 1


createAttribute creates a new attribute node, and returns it.


attribute = document.createAttribute(name) 


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



<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";
  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"


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


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.


See also

Document Tags and Contributors

 Last updated by: Sheppy,