From the course: Learning App Building with Vanilla JavaScript
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Add event listeners with vanilla JavaScript - JavaScript Tutorial
From the course: Learning App Building with Vanilla JavaScript
Add event listeners with vanilla JavaScript
- [Instructor] The first event listener in our code is on line 18, for the element with the class value, forecast button. This is the button that users click to look up the weather for a location they've entered. So, we'll comment out the code on line 18, and then on a new line, our first step is to replace the jquery selector with a vanilla javascript selector. Because this is a single element, we'll use document.queryselector and we'll use the same selector as in the jquery statement, which is .forecast-button. Then we're going to type a dot, and we're going to specify the add event listener method for the selected element. In the parens, we need to specify three arguments. So first, we're going to specify a string that indicates the event to listen for. For this button, that event is click. Then a comma, and then the second argument, and that's a function or a function call. So in this case, we're going to use an anonymous function. We already have this in place for the jquery…
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.