screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <title>Add/Remove Class on Click</title> </head> <body> <h3>Js Add Remove Class to Div</h3> <div id="myElement" class="">This is Some Text </div> <button id="button">Add Highlight</button> <script> const element = document.getElementById("myElement"); const button = document.getElementById("button"); button.addEventListener("click", function () { if (element.classList.contains("highlight")) { element.classList.remove("highlight"); button.textContent = "Add Highlight"; } else { element.classList.add("highlight"); button.textContent = "Remove Highlight"; } }); </script> <style> .highlight { padding: 10px; background-color: yellow; } </style> </body> </html>