Get your own website Result Size: 625 x 565
x
 
<!DOCTYPE html>
<html>
<style>
.democlass {color: red;}
</style>
<body>
<h1>The Document Object</h1>
<h2>The createAttribute() and setAttributeNode() Methods</h2>
<p>Click "Add" to create a class attribute and add it to first h1 element.</p>
<button onclick="myFunction()">Add</button>
<script>
function myFunction() {
// Create a class attribute:
const att = document.createAttribute("class");
// Set a value of the class attribute
att.value = "democlass";
// Add the class attribute to the first h1;
document.getElementsByTagName("h1")[0].setAttributeNode(att);
}
</script>
</body>
<!-- Mirrored from www.w3schools.com/jsref/tryit.asp?filename=tryjsref_document_createattribute by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 05 Sep 2022 15:18:43 GMT -->
</html>