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…
Many events bubble from their target through ancestor elements. This enables a container to observe interactions on its children. It also explains why more than one listener may run for a single action. stopPropagation prevents later propagation; preventDefault cancels a default action and is a different operation. Use propagation control deliberately rather than adding it everywhere. Events can bubble through ancestors. target identifies the originating node, while currentTarget identifies the listener’s node.
const trace = document.querySelector('#trace');
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');Run the preview tests; inspect the live page.
Many events bubble from their target through ancestor elements. This enables a container to observe interactions on its children. It also explains why more than one listener may run for a single action. stopPropagation prevents later propagation; preventDefault cancels a default action and is a different operation. Use propagation control deliberately rather than adding it everywhere.
const trace = document.querySelector('#trace');Declare trace with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
const trace = document.querySelector('#trace');
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');
console.log(document.querySelectorAll("*").length);Interact with the preview; the diagnostic displays its actual element count.
Many events bubble from their target through ancestor elements. This enables a container to observe interactions on its children. It also explains why more than one listener may run for a single action. stopPropagation prevents later propagation; preventDefault cancels a default action and is a different operation. Use propagation control deliberately rather than adding it everywhere. Events can bubble through ancestors. target identifies the originating node, while currentTarget identifies the listener’s node. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
const trace = document.querySelector('#trace');Declare trace with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');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 trace = document.querySelector('#trace');
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');
console.log(document.querySelectorAll("button").length);Interact with the preview; the diagnostic displays its actual element count.
Many events bubble from their target through ancestor elements. This enables a container to observe interactions on its children. It also explains why more than one listener may run for a single action. stopPropagation prevents later propagation; preventDefault cancels a default action and is a different operation. Use propagation control deliberately rather than adding it everywhere. Events can bubble through ancestors. target identifies the originating node, while currentTarget identifies the listener’s node. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
const trace = document.querySelector('#trace');Declare trace with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');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.
Append inner then outer to #trace using listeners on the button and its container.
// Append inner then outer to #trace using listeners on the button and its container. // Write your JavaScript below.
const trace = document.querySelector('#trace');
document.querySelector('#inner').addEventListener('click', () => trace.textContent += 'inner ');
document.querySelector('#outer').addEventListener('click', () => trace.textContent += 'outer');Attach two ordinary click listeners without stopping propagation.
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.