screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://cdn.materialdesignicons.com/5.9.55/css/materialdesignicons.min.css"> </head> <body> <div id="app"> <h1>How to Change Icon on Click in Javascript</h1> <button id="toggleButton"><i class="mdi mdi-fullscreen"></i></button> </div> <script> document.addEventListener("DOMContentLoaded", function () { const toggleButton = document.getElementById("toggleButton"); let isIconClicked = false; const handleClick = () => { isIconClicked = !isIconClicked; toggleButton.style.backgroundColor = isIconClicked ? "#ff0000" : "#4caf50"; toggleButton.innerHTML = isIconClicked ? '<i class="mdi mdi-fullscreen-exit"></i>' : '<i class="mdi mdi-fullscreen"></i>'; }; toggleButton.addEventListener("click", handleClick); }); </script> <style> * { box-sizing: border-box; } #app { text-align: center; margin: 20px; } #toggleButton { background-color: #4caf50; color: #fff; padding: 10px 20px; font-size: 16px; border: none; border-radius: 4px; cursor: pointer; transition: background-color 0.3s ease; } #toggleButton:hover { background-color: #45a049; } #toggleButton i { font-size: 25px; } </style> </body> </html>