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

145
Views
How can I assign the link of image that I got with API to src of image?

I can't assign the link of image that I got with API to src of image. I'm using fetch to get info, (I'm new with APIs).

    fetch("https://restcountries.com/v3.1/all")
        .then(respons => respons.json())
        .then(b => {
         let pic = b[0].flags.png;
         document.getElementById("img").src =  "pic";
        })
<img src="" alt="" id="img">

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

0

There’s a problem with your code. You created the variable named pic. But instead of assigning variable to src you are assigning "pic" string. The correct code is:

fetch("https://restcountries.com/v3.1/all")
        .then(respons => respons.json())
        .then(b => {
         let pic = b[0].flags.png;
         document.getElementById("img").src =  pic;
        })

Note that I removed quotes from pic variable.

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!