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

Add an event listener - JavaScript Tutorial

From the course: JavaScript: Ajax and Fetch

Start my 1-month free trial

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…

Contents