Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Uncaught (in promise) TypeError: img.src is not a function

I got this error while working on javascript what I wanted to do is that I want to add img src in javascript through data fetched using an API

my Javascript code is


const img = document.getElementById("image");
const input = document.querySelector("input");
const weather = document.getElementById("el");
const form = document.querySelector("form");

form.addEventListener("submit", (el) => {
  el.preventDefault();
  const city = input.value;
  fetch(
    `http://api.weatherstack.com/current?access_key=113582164e8a6873ca56cbba32c571ee&query=${city}`,
    {
      referrerPolicy: "unsafe-url",
    }
  )
    .then((res) => {
      console.log(res);
      return res.json();
    })
    .then((apiResponse) => {
      console.log(apiResponse);
      weather.classList.remove("op");
      img.src(apiResponse.current.weather_icons[0]);
   
    });
});

My Html Code is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div id="el" class="weather op">
      <img src="" alt="" id="image" />
</div>

Everything is working I have tested API its giving correct result there is an issue with add src to the img

>Solution :

Use img.src = apiResponse.current.weather_icons[0]; instead of img.src(apiResponse.current.weather_icons[0]);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading