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

Time:10-05

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

<div id="el" >
      <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

CodePudding user response:

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

CodePudding user response:

let img_element = document.getElementById("image");
img_element.src = apiResponse.current.weather_icons[0];
  • Related