Event Listeners
Explore how to use event listeners in JavaScript to handle user interactions such as clicks, mouse movements, and keyboard inputs. Understand modifying element properties dynamically and implementing responsive behavior in your web pages.
Understanding user interaction events
When a user visits your page, every interaction they have with the page (e.g., a mouse click, scrolling, window resizing, etc.) is recorded as a series of events. As programmers, we can use these events to bind JavaScript code to specific user interactions.
Hopefully, this code is making much more sense to you now that you’ve learned a bit about how JavaScript works.
The variable
buttonstores an element node object that represents the HTML button.Since
buttonis an object, we can define properties on it.the
onclickproperty acts as an event listener:onclickis defined as a function, and that function will execute whenever a click event occurs on the<button>element.
Exercise
Modify the onclick event listener to add a <p> element that says Clicked! every time the <button> is clicked.
Modifying elements using this
Let’s say instead of pop-up alert on the screen, we wanted to change the color of the <button> every time we clicked it. How could we access the <button>'s properties from inside its event listener?
Since the button variable is an object, we can use the this keyword from within the object’s methods to access its properties:
In the example above, we attach a function to the button's onclick property, which creates an event listener.
From within the onclick function, the this keyword refers to the button object, since the function is attached to the button object. Using this from within an event listener is a common pattern that we will explore further in the next few examples.
Exercise
You can access a DOM element’s font color using Element.style.color. Use an event listener (and the random color generator) to change the button’s font color every time it is clicked.
More event listeners
There are many different kinds of events that occur in the browser. A few examples include the following.
Mouse events
Events that occur with a user’s mouse.
ondblclick: Occurs when an element is double clicked.onmouseenter: Occurs when the mouse pointer moves onto an element.onmouseleave: Occurs when the mouse pointer moves away from an element.
Keyboard events
Events related to keypresses on the user’s keyboard.
onkeypress: Occurs when a key is pressed on the user’s keyboard.onkeydown: Occurs when a user is pressing down on a key.onkeyup: Occurs when a user releases a key press.
Form events
Events related to interactions with form elements, such as <input>.
onfocus: Occurs when focus is placed on an<input>field.onchange: Occurs when an<input>'s value is changed (useful forradioorcheckboxtype inputs).oninput: Occurs when a user provides input to an element (useful fortexttype inputs or<textarea>elements).
For instance, we could bind data from a text <input> element to an <h1> using the oninput event listener:
We could check whether or not a checkbox input is checked using an onchange event listener:
Exercise
Let’s start to create a to-do list. Given the following HTML, use JavaScript to add a list item to the to-do list when a user clicks the “Add” button.
- The contents of the list item should be the input provided.
- If the user clicks “Add” and there is no input, a list item should not be added.
- The to-do input should be reset after the list item is added.