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…
addEventListener connects an event type with a callback. Pass the callback function rather than the result of calling it. The function runs later when the event occurs. A native button already supports keyboard activation, so it is a better starting point than making a clickable div. Keep visible feedback close to the action and meaningful to the user. An event listener registers behavior for later. Adding the listener does not immediately execute its handler.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });Run the preview tests; inspect the live page.
addEventListener connects an event type with a callback. Pass the callback function rather than the result of calling it. The function runs later when the event occurs. A native button already supports keyboard activation, so it is a better starting point than making a clickable div. Keep visible feedback close to the action and meaningful to the user.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });Register a handler for the named event. The handler runs later when the event is dispatched, not at registration time.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });
console.log(document.querySelectorAll("*").length);Interact with the preview; the diagnostic displays its actual element count.
addEventListener connects an event type with a callback. Pass the callback function rather than the result of calling it. The function runs later when the event occurs. A native button already supports keyboard activation, so it is a better starting point than making a clickable div. Keep visible feedback close to the action and meaningful to the user. An event listener registers behavior for later. Adding the listener does not immediately execute its handler. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });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.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });
console.log(document.querySelectorAll("button").length);Interact with the preview; the diagnostic displays its actual element count.
addEventListener connects an event type with a callback. Pass the callback function rather than the result of calling it. The function runs later when the event occurs. A native button already supports keyboard activation, so it is a better starting point than making a clickable div. Keep visible feedback close to the action and meaningful to the user. An event listener registers behavior for later. Adding the listener does not immediately execute its handler. The final diagnostic counts actual matching elements. Read querySelectorAll as a selection and length as the collection count.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });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.
On #button click, set #message to Hello!
// On #button click, set #message to Hello! // Write your JavaScript below.
document.querySelector('#button').addEventListener('click', () => { document.querySelector('#message').textContent = 'Hello!'; });Use an arrow function as the second argument.
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.