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>Create a style attribute that adds a red color to the h1 element.</p> <script> // Create a class attribute: const att = document.createAttribute("style"); // Set the value of the class attribute att.value = "color:red"; // Add the class attribute to the first h1; const h1 = document.getElementsByTagName("h1")[0]; h1.setAttributeNode(att); </script> </body> </html>