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
  • Updating
  • Removing the First Result
  • TEST
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times
  4. 05-displayResults

05-Results Update

In this module, we'll work on updating the dom after a new search.

Updating

Right now, if we were to run another search without refreshing the page, the newest JSON would append to the bottom of the results. Try this: 1. Open the app. 2. Open Chrome tools. 3. Do a search and get the results. 4. Without refreshing the page, do another search. 5. Notice that the results are appended to the bottom.

Removing the First Result

At the top of the displayResults function, add the following code. Again, note that some code is omitted:

function displayResults(json) {
  while (section.firstChild) {
      section.removeChild(section.firstChild); //1

  }
};

Here's what's happening: 1. We run the displayResults function each time the button gets pressed. In this chunk of code, we are checking to see if the section element has any child elements. If the section.firstChild is true, then we call removeChild on the section variable, which targets the section element in the html file. This simply will clear out any child elements that are in the section.

Simply put, the while loop will clear out any articles before new search results are added.

TEST

Test the app by doing the following: 1. Open the app. 2. Open Chrome tools on the "elements" tab. 3. Do a search and get the results. 4. Without refreshing the page, do another search. 5. You should see the elements in the section disappear, and the new elements should be created.

Previous04-Results NavigationNext06-Keywords

Last updated 7 years ago