Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!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> </html>