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 Element Object</h1> <h2>The setAttributeNode() Method</h2> <p>Click "Change" to set the class attribute node of the first header.</p> <button onclick="myFunction()">Change</button> <script> function myFunction() { const attr = document.createAttribute("class"); attr.value = "democlass"; const element = document.getElementsByTagName("H1")[0]; element.setAttributeNode(attr); } </script> </body> </html>