Document createAttribute() Method - Javascript DOM

Javascript examples for DOM:Document createAttribute

Description

The createAttribute() method creates an attribute and returns the attribute as an Attr object.

Parameter Values

Parameter TypeDescription
attributename Attr object Required. The name of the attribute you want to create

Return Value:

A Node object, representing the created attribute

The following code shows how to Create a class attribute, with the value "democlass", and insert it to an <h1> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<a id="myAnchor">A Link: go to java2s.com</a>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/* w ww .  j  av  a 2s.  c o m*/
    var anchor = document.getElementById("myAnchor");
    var att = document.createAttribute("href");
    att.value = "http://www.java2s.com";
    anchor.setAttributeNode(att);
}
</script>

</body>
</html>

Related Tutorials