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

116
Views
Im trying to pull data from an API and render it to my HTML using Javascript. I think Im close

Ive been trying to pull data from an "Current News Source" API structured in JSON using Javascript. Im in the testing phases right now.

On click I want to have my HTML element named "title" updated to the "title from the News JSON file. Heres what I have so far..

The HTML


<h3 class="title text-center" id="title" name="title">Russia accuses US of interfering in vote</h3>

<button type="button" class="btn btn-outline-dark text-white bg-muted" id="searchButton">Search</button>

The Javascript

const title = document.getElementById("title");
const searchButton = document.getElementById("searchButton");

searchButton.addEventListener("click", getRandomTitle)

function getRandomTitle() {
    fetch('https://api.currentsapi.services/v1/latest-news?q=keywords=Biden&apiKey=12345678910')
    .then(res => res.json())
    .then(data => {
        title.innerHTML = data.news.title

});

}

The JSON file


{
  "status": "ok",
  "news": [
    {
      "id": "4e67842f-018d-4744-aa02-d21da55c278d",
      "title": "Early Matter Domination from Long-Lived Particles in the Visible Sector. (arXiv:2108.13136v2 [hep-ph] UPDATED)"

...

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

data.news is an array of object, to access the specific title you have to use the corresponding index.

Also, I will suggest you to use HTMLElement.innerText or Node.textContent to set the plain text (not htmlString) to a HTML element:

title.textContent = data.news[0].title; // get the title from first news object
about 4 years ago · Juan Pablo Isaza Report

0

From the JSON file, it looks like data.news is an array.
Try data.news[0].title

about 4 years ago · Juan Pablo Isaza Report

0

Try to use data.news[0].title because it seems you are getting array of object as result in data.news

function getRandomTitle() {
fetch('https://api.currentsapi.services/v1/latest-news?q=keywords=Biden&apiKey=12345678910')
.then(res => res.json())
.then(data => {
    title.innerHTML = data.news[0].title   // Specify the index for the array

});
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!