Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

45
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">

7 months 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.

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs