From the course: JavaScript: Ajax and Fetch
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Add an event listener - JavaScript Tutorial
From the course: JavaScript: Ajax and Fetch
Add an event listener
- [Instructor] Now I have a function that will actually check if the address field, city field and state field are all filled, and then submit my request. So the next thing I need to do to finish this up is to make sure that that function is called in response to the blur event on each of these three fields. Now, I want to call my check completion function in response to the blur event on three different elements. To do that with JavaScript I use the add event listener method, which takes two arguments, an event type as a string, and either a function name or an inline anonymous function. So first I want to add an event listener to the element I referenced with the address field variable. So addressField.addEventListener, and I'll pass the event name blur as a string, and then specify checkCompletion as the function. And I want to do the same thing for the other two fields, because if a user changes info in any of these fields, the query needs to be resubmitted. So I can do…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
What is the DOM?3m 19s
-
(Locked)
Select elements with vanilla JavaScript3m 43s
-
(Locked)
Request data in response to an event4m 14s
-
(Locked)
Add an event listener2m 12s
-
(Locked)
Work with JSON data6m 52s
-
(Locked)
Modify form values with vanilla JavaScript4m 58s
-
(Locked)
Modify element content with vanilla JavaScript5m 49s
-
(Locked)
Challenge: Modify the DOM58s
-
(Locked)
Solution: Modify the DOM7m 55s
-
-
-
-
-