Page Loading...
Run
Break Lines
Result
<!DOCTYPE html> <html> <head> <style> .yellow{ background-color: yellow; } .red{ color: red; } </style> </head> <body> <div id='myDiv' class='red'>This is a div element</div><br> <button onclick='addClass()'>Add Class</button> <script> let div = document.getElementById('myDiv'); function addClass(){ // Add class .yellow to the div div.setAttribute('class','yellow'); } </script> </body> </html>