Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
A disclosure button reveals or hides related content. Use a native button, aria-expanded for its state and aria-controls to identify the panel. The hidden property controls visibility and normally removes the panel from layout and accessibility exposure. Keep the visual and accessibility states synchronized in both directions. A CSS-only color change is not enough to communicate the state. Accessible controls need a clear name and synchronized state. A native button is keyboard-operable without extra key handlers.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });Run the preview tests; inspect the live page.
A disclosure button reveals or hides related content. Use a native button, aria-expanded for its state and aria-controls to identify the panel. The hidden property controls visibility and normally removes the panel from layout and accessibility exposure. Keep the visual and accessibility states synchronized in both directions. A CSS-only color change is not enough to communicate the state.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');Declare button with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });
console.log(document.querySelectorAll("*").length);Interact with the preview; the diagnostic displays its actual element count.
A disclosure button reveals or hides related content. Use a native button, aria-expanded for its state and aria-controls to identify the panel. The hidden property controls visibility and normally removes the panel from layout and accessibility exposure. Keep the visual and accessibility states synchronized in both directions. A CSS-only color change is not enough to communicate the state. Accessible controls need a clear name and synchronized state. A native button is keyboard-operable without extra key handlers. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');Declare button with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
console.log(document.querySelectorAll("*").length);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });
console.log(document.querySelectorAll("button").length);Interact with the preview; the diagnostic displays its actual element count.
A disclosure button reveals or hides related content. Use a native button, aria-expanded for its state and aria-controls to identify the panel. The hidden property controls visibility and normally removes the panel from layout and accessibility exposure. Keep the visual and accessibility states synchronized in both directions. A CSS-only color change is not enough to communicate the state. Accessible controls need a clear name and synchronized state. A native button is keyboard-operable without extra key handlers. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');Declare button with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
console.log(document.querySelectorAll("button").length);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Toggle #panel.hidden and synchronize aria-expanded on every click.
// Toggle #panel.hidden and synchronize aria-expanded on every click. // Write your JavaScript below.
const button = document.querySelector('#toggle'); const panel = document.querySelector('#panel');
button.addEventListener('click', () => { panel.hidden = !panel.hidden; button.setAttribute('aria-expanded', String(!panel.hidden)); });Use panel.hidden as the state and invert it.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.