const toggleButton = document.getElementById('toggleButton'); const targetElement = document.getElementById('myElement'); // Attach an event listener to the button toggleButton.addEventListener('click', () => { // Toggles the 'active' class: adds if not present, removes if present targetElement.classList.toggle('active'); // Optional: Check current state if (targetElement.classList.contains('active')) { console.log('Element now has active class.'); } else { console.log('Element no longer has active class.'); } }); // Example HTML structure and CSS (for context): // //
Click button to toggle class
//