Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
I used fetch() method to load data from external server. All data are being loaded properly. But I came across dificulity when I use querySelector

I'm trying to load data from an external server and render it on my web page by using javascript. I used fetch(url) to load the data. Data is being loaded properly; I can see all of it in the console. What happens is that, when I use document.querySelector(), only 1 article is shown on my web page despite there being 10 articles. Then, I used document.querySelectorAll(), but nothing showed on my web page and there were no errors, no information in the console as well. What I did was this:

let url = "https://gnews.io/api/v4/top-headlines?lang=en&max=50&token=TOKEN_HERE";
    
fetch(url)
    .then(function(response) {
        return response.json();
    })
    .then(function(details) {
        let allArticles = details.articles;
        allArticles.forEach(function(article) {
            let header = document.querySelector('.heading');
            header.innerText = article.title;

            let description = document.querySelector('.description');
            description.innerText = article.description;

            let content = document.querySelector('.content');
            content.innerText = article.content;
        });
    });

HTML:

<body>
    <h4 class="heading"></h4>
    <p class="description"></p>
    <p class="content"></p>
    <!-- ... -->
</body>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You will need to generate DOM. Use your HTML structure as template for each article.

const buildArticle = (id, title, desc, content) => {
  return `
    <div id="article-${id}"">
    <h4 class="heading">${title}</h4>
    <p class="description">${desc}</p>
    <p class="content">${content}</p>
 </div>
`;
};

let url =
  "https://gnews.io/api/v4/top-headlines?lang=en&max=50&token=368273b6710cecf2476380400a7635c2";

fetch(url)
  .then(function (response) {
    return response.json();
  })
  .then(function (details) {
    let allArticles = details.articles;

    document.getElementById("app").innerHTML = allArticles
      .map(function (article, idx) {
        return buildArticle(
          idx,
          article.title,
          article.description,
          article.content
        );
      })
      .join("");
  });
<div id="app"></div>

alternate way

const createArticle = (id, title, desc, content) => {
  const article = document
    .querySelector("#article-template")
    .content.cloneNode(true);
  article.querySelector(".article").id = `article-${id}`;
  article.querySelector(".heading").textContent = title;
  article.querySelector(".description").textContent = desc;
  article.querySelector(".content").textContent = content;
  return article;
};

const url =
  "https://gnews.io/api/v4/top-headlines?lang=en&max=50&token=368273b6710cecf2476380400a7635c2";

fetch(url)
  .then((res) => res.json())
  .then((details) =>
    details.articles.forEach((article, idx) =>
      document
        .getElementById("app")
        .appendChild(
          createArticle(
            idx,
            article.title,
            article.description,
            article.content
          )
        )
    )
  );
<div id="app"></div>

<template id="article-template">
  <div class="article">
    <h4 class="heading"></h4>
    <p class="description"></p>
    <p class="content"></p>
  </div>
</template>;

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!