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 an Anchor Element
  • Clickable Links
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times
  4. 05-displayResults

03-Link Display

In this module, we'll make the links show up as headings.

Adding an Anchor Element

Let's add more code and break it down afterwards:

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

  if(articles.length === 0) {
    console.log("No results");
  } else {
    for(let i = 0; i < articles.length; i++) {
      let article = document.createElement('article');
      let heading = document.createElement('h2');
      let link = document.createElement('a'); //1

      let current = articles[i]; //2
      console.log("Current:", current); //3

      link.href = current.web_url; //4
      link.textContent = current.headline.main; //5

      article.appendChild(heading);
      heading.appendChild(link); //6
      section.appendChild(article);
    }
  }
};

Here's the analysis of what we've done: 1. We create a link variable that is going to use the a element, the anchor tag which will allow us to create an 'href' link. 2. We create a current variable that holds the data of the current article as we iterate. 3. We log the current data so that we can see it in the console. 4. Since link is an a element, we need to attach an href property to it. current.web_url grabs the hyperlink for the current article out of the json result. This will set the value for the link.href each time we iterate. 5. The text that we'll use in link.textContent is set to the value of current.headline.main, which is part of the json object from the NYT API. You can see this when you drill down into the data:

6. Finally, we call the appendChild method on the heading element again. This will append a link as a child element in the DOM inside of each h2. See the screenshot for orientation:

Clickable Links

You should have two things at this point: 1. Here's what you should be seeing by now.

  1. The links that you see are the headings, which should be clickable.

Previous02-Working with JSONNext04-Results Navigation

Last updated 7 years ago

Screenshot
Screenshot
Screenshot