Element setAttribute() Method - Change class attribute for h1 - Javascript DOM

Javascript examples for DOM:Element setAttribute

Description

Element setAttribute() Method - Change class attribute for h1

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.democlass {// w w  w.  jav a 2 s  .c  o m
    color: red;
}
</style>
</head>
<body>

<h1>Hello World</h1>

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

<script>
function myFunction() {
    var attr = document.createAttribute("class");
    attr.value = "democlass";
    var h = document.getElementsByTagName("H1")[0];
    h.setAttributeNode(attr);
}
</script>

</body>
</html>

Related Tutorials