Complete the code to add a click event listener to the button.
document.getElementById('toggle-btn').addEventListener('[1]', () => { document.documentElement.classList.toggle('dark'); });
The click event triggers when the user clicks the button, which is needed to toggle dark mode.
Complete the code to select the button element by its ID.
const toggleBtn = document.[1]('toggle-btn');
getElementsByClassName returns a collection, not a single element.querySelectorAll returns a list, not a single element.querySelector works but requires a selector string with '#'.getElementById selects a single element by its unique ID, which is perfect for our toggle button.
Fix the error in toggling the dark mode class on the root element.
document.documentElement.classList.[1]('dark');
add only adds the class but never removes it.remove only removes the class but never adds it.contains only checks if the class exists, does not change it.toggle adds the class if missing, or removes it if present, perfect for switching dark mode on and off.
Fill both blanks to create a button with accessible label and Tailwind styles for dark mode.
<button id="toggle-btn" aria-label="[1]" class="[2]"> Toggle Dark Mode </button>
The aria-label describes the button for screen readers. The Tailwind classes style the button with light background normally and dark background in dark mode.
Fill all three blanks to complete the JavaScript code that toggles dark mode and saves preference in localStorage.
const btn = document.getElementById('toggle-btn'); btn.addEventListener('click', () => { document.documentElement.classList.[1]('dark'); if(document.documentElement.classList.contains('dark')) { localStorage.setItem('[2]', '[3]'); } else { localStorage.removeItem('[2]'); } });
add instead of toggle causes only adding, no removal.toggle switches the dark class. The key theme stores the preference. The value dark marks dark mode active.