JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times

03-Event Listeners

Previous02-VariablesNext04-fetchResults

Last updated 7 years ago

It's important for you to know that the DOM is listening for all kinds of . In this module, we'll send some event listener methods so that the DOM can listen for activity.

target.addEventListener()

We'll use the addEventListener() method here. This method will help us identify a target and then add an event listener on that target. Event targets can be an element, the document object, the window object, or any other object that supports events.

Let's discuss where events are going to happen in our app: 1. We want to submit a form that contains a query: "Sports", "Politics", "Weather", etc. 2. We want to be able to toggle through the results when we click on the next or previous button.

Let's look at the syntax of using this method:

        //1                     //2   
searchForm.addEventListener('submit', fetchResults); 
nextBtn.addEventListener('click', nextPage); //3
previousBtn.addEventListener('click', previousPage); //3

So here's how this works: 1. First of all, remember that the searchForm variable targets the form element in the html page:

const searchForm = document.querySelector('form');

We use the searchForm variable and call the addEventListener method on it. We want to listen for things happening on the particular element, which in this case is the searchForm.

  1. The event that we're looking for is the submit event. This is an HTML form event. Note that the submit event fires on a . When this event happens (the form is submitted by pressing the submit button), we will fire off a function called fetchResults, the second parameter in the function.

  2. The same is true for the other two items, except that they called are click events. When we click on the next button, we fire off a function called nextPage. When we click on the previous button, we run previousPage.

Note that the click event is fired when a pointing device button (usually a mouse's primary button) is pressed and released on a single element.

events
form, not a button