From the course: JavaScript Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Event listeners

Event listeners - JavaScript Tutorial

From the course: JavaScript Essential Training

Start my 1-month free trial

Event listeners

- [Instructor] An EventListener is exactly what it sounds like: A method added to a target, usually an element, that listens for a specific event and then calls back a function when that event is detected. In this code example, we have an EventListener appended to a button. When the button is clicked, we log an event in the console. So the structure of an EventListener is always the same. We start by grabbing an event target. This can be a window object or the document object or any element within the dom. Next, we add an EventListener with the addEventListener method. This method takes two main arguments. First, the event we want to listen to in quotation marks. This can be any of the events listed in the MDN events reference, and second, a callback function to run when that event is triggered. This can be either an inline anonymous function or a call to an external function. And remember, this is a callback function,…

Contents