How to create new attribute node to an element with Document.createAttribute() in JavaScript

2 Answers

0 votes
<!DOCTYPE html>
<html>
<head>
<style>
.seth1color {
    color: blue;
}
</style>
</head>
<body>
<h1>New Attr</h1>
<script type="text/JavaScript"> 
var attr = document.createAttribute("class");
attr.value = "seth1color";
var h1 = document.getElementsByTagName("H1")[0];
h1.setAttributeNode(attr); 

/*
run:

New Attr
*/

</script>
</body>
</html>

 



answered Jun 12, 2016 by avibootz
0 votes
<!DOCTYPE html>
<html>
<head>
<style>
.seth1color {
    color: blue;
}
</style>
</head>
<body>
<h1>New Attr</h1>
<button onclick="SetAttr()">Create Attr</button>
<script type="text/JavaScript"> 
function SetAttr() 
{
    var attr = document.createAttribute("class");
    attr.value = "seth1color";
    var h1 = document.getElementsByTagName("H1")[0];
    h1.setAttributeNode(attr); 
}

/*
run:

New Attr
*/

</script>
</body>
</html>

 



answered Jun 12, 2016 by avibootz
...