Javascript examples for DOM:Document createAttribute
The createAttribute() method creates an attribute and returns the attribute as an Attr object.
Parameter | Type | Description |
---|---|---|
attributename | Attr object | Required. The name of the attribute you want to create |
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:
<!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>