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
  • Adding the Navigation
  • Analysis
  • Screenshot
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times
  4. 05-displayResults

04-Results Navigation

In this module, we'll add navigation for showing multiple results, if we get more than 10 in our response.

Adding the Navigation

Add the following code after the articles variable in displayResults. Note that some code has been omitted from the below code, but you should still keep them in your code (i.e. don't delete the previous code of article.length === 0:

function displayResults(json) {
  let articles = json.response.docs;

  if(articles.length === 10) {
    nav.style.display = 'block'; //shows the nav display if 10 items are in the array
  } else {
    nav.style.display = 'none'; //hides the nav display if less than 10 items are in the array
  }

};

Analysis

Here we've added an if-else that does the following things: 1. We check to see if articles.length is greater than 10. If it is, we will set the CSS display to block. Notice that we are targeting the nav element created in the nav variable at the top of the page. Remember that this nav variable is targeting the nav in our original html file, we have this element:

     <div class="results">
        <nav>
          <button class="prev">Previous 10</button>
          <button class="next">Next 10</button>
        </nav>

        <section>
        </section>
      </div>

Essentially, if we get ten results in the JSON data, it will display the nav element in the dom with the two buttons, Previous 10 & Next 10.

If we do not have at least 10 results, we set the CSS display property to none. This means, of course, that the buttons won't show. If we only get 7 or 8 results, we don't need buttons to navigate to the next 10 results.

Screenshot

Here's what you should be seeing now when the results are 10 or more. Note that the buttons will not be functional until later.

Previous03-Link DisplayNext05-Results Update

Last updated 7 years ago

Screenshot