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

173
Visualizações
How can I get my image replacing javascript code to work in an external .js file

So I'm new to web development (career change) and I'm hitting a roadblock that I can't seem to get past.

I'm trying to write a script that will change the img src in my HTML. When I use inline JavaScript it does what I want and replaces the image:

<img id="book1" src="placeholder" alt="Book1">
<h5 class="title">TITLE by Author Name</h5>
<p>Book description</p>
<div class="center">
  <button type="button" class="amz-btn" id="btn1" onclick="showImage()">Buy from Amazon</button>
  <script type="text/javascript">
    function showImage() {
      let image = document.getElementById("book1");
      if (image.src.match("placeholder")) {
        image.src = "https://storage.googleapis.com/du-prd/books/images/9781538728529.jpg";
      } else {
        console.log("This is frustrating");
      }
    }
  </script>

But when I transfer it to my external .js file I get a syntax error saying showImage is not defined. How can I resolve this?

Here is the code as written just in the external file

document.getElementById("btn1").addEventListener("click", showImage)
function showImage() {
  let image = document.getElementById("book1");
  if (image.src.match("placeholder")) {
    image.src = "https://storage.googleapis.com/du-prd/books/images/9781538728529.jpg";
  }
  else {
    console.log("This is frustrating");
  }
}

Someone suggested that I use addEventListener rather than onclick in the HTML so I've done that but I'm confident it's in entirely the wrong place and doing the wrong thing

I've tried running the following in the browser console and it runs perfectly but I still can't get it to work from the external file

      const img = "https://storage.googleapis.com/du-prd/books/images/9781538728529.jpg";

      document.getElementById("btn1").addEventListener("click", showImage);

      function showImage() {
        document.getElementById("book1").src = img;
      }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Issue resolved!

      const img = "https://storage.googleapis.com/du-prd/books/images/9781538728529.jpg";

      document.getElementById("btn1").addEventListener("click", showImage);

      function showImage() {
        document.getElementById("book1").src = img;
      }

The reason the above wasn't working was because I did something wrong with the window.addEventListener('load', (event) => { // all code here; }); but after removing that the image appears on a button click

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