Events in the browser

Knowledge Videos

Short animated explainers, organised by subject.

Events in the browser

An event is something that happens on the page. addEventListener waits for it and runs a handler function, shown next to the same idea in Scratch.

Transcript

You already know events from Scratch. In JavaScript, an event is something that happens on the page, like a click, a key press, or a form being submitted.

To react, you add an event listener. Pick an element, call add event listener, and give it the name of the event, click, and a function to run. That function is the event handler.

Now, when the button is clicked, the browser runs the function, and the paragraph changes. You do not call the function yourself. The browser calls it when the event happens.

Here is the same idea in Scratch: when this sprite is clicked, change score by one. Two languages, one idea.

Remember: an event is something that happens. An event listener waits for it, and the event handler is the function that runs when it does.

More in this series