Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
The src of img object doesn't work specificly for me

im trying to make an src for an img element that i have in my HTML file, but for some reason, when I try to insert a source into an image, the workspace does not recognize the img object as an image object, and therefore does not allow src to be inserted there.

It is important to emphasize that this problem only exists in the work environment I work with (WebStorm). When I tried to put the code into an online work environment, it worked great.

My attempt to put src in WebStorm despite the problem like this:

HTML:

<img id="img" src="https://source.unsplash.com/random">

JS:

let img = document.getElementById("img");
img.src = "https:\/\/images.dog.ceo\/breeds\/terrier-toy\/n02087046_3843.jpg";

But it just made the work environment react with this error:

Cannot set properties of null (setting 'src')

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

As written in the question, there are 2 (no, 3) options to fix the error.

Option 1: Normal string quote

img.src = "https://images.dog.ceo/breeds/terrier-toy/n02087046_3843.jpg";

Option 2: Add quotes around your original escaped character string

img.src = "https:\/\/images.dog.ceo\/breeds\/terrier-toy\/n02087046_3843.jpg";

Option 3: Set the attribute

img.setAttribute("src", "https://images.dog.ceo/breeds/terrier-toy/n02087046_3843.jpg");
about 4 years ago · Juan Pablo Isaza Relatório

0

Check out this code. I've added setTimeout for 2 second just to visualize. You can remove setTimeout from here in your case.

function changeImage() {
  setTimeout(function(){
    var image = document.getElementById("img")
    image.src = "https://i.imgur.com/xNE8K6Q.jpeg"
  }, 2000)
}

changeImage()
<html>
  <body>
      <img id="img" src="https://i.imgur.com/hk1iahZ.jpeg" height="100" width="100" />
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Attributes of tags always has the string value so put your url as string. As per your code, you can try this:

let img = document.getElementById("img");
img.src = "https://images.dog.ceo/breeds/terrier-toy/n02087046_3843.jpg";
<img id="img" src="https://source.unsplash.com/random">

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda