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

241
Visualizações
JavaScript file name selector application, only selecting first element

JavaScript file name selector application, only selecting first element.

There are twelve images with a class of foto, each labelled step1.png, step2.png etc. When one of the images is clicked, the name of the image file should be displayed, less the png file extension. For example, step1.png become step1. It all works as expected but only ever displays the first file name no matter which image is clicked on. I would be very grateful for any clues to my error with this code.

<title>Photo Files</title>
    <style>
      .box {
        margin: auto;
        width: 200px;

        background-color: lightgray;
      }
      .box p {
        color: green;
        font-weight: bold;
      }
    </style>
  </head>
  <body>
    <h1>Selects image file name</h1>
    <div class="box">
      <img class="foto" src="images/step1.png" alt="" />
      <img class="foto" src="images/step2.png" alt="" />
      <img class="foto" src="images/step3.png" alt="" />
      <img class="foto" src="images/step4.png" alt="" />
      <img class="foto" src="images/step5.png" alt="" />
      <img class="foto" src="images/step6.png" alt="" />
      <img class="foto" src="images/step7.png" alt="" />
      <img class="foto" src="images/step8.png" alt="" />
      <img class="foto" src="images/step9.png" alt="" />
      <img class="foto" src="images/step10.png" alt="" />
      <img class="foto" src="images/step11.png" alt="" />
      <img class="foto" src="images/step12.png" alt="" />

      <p class="picName">Name of file:</p>
      <br />
      <hr />
    </div>

    <script>
      var pic = document.querySelectorAll(".foto");
      for (var i = 0; i < pic.length; i++) {
        pic[i].addEventListener(
          "click",
          function () {
            var fullPath = document.querySelector(".foto").src;

           
            var filename = fullPath.replace(/^.*[\\\/]/, "");

            
            document.querySelector(".picName").textContent =
             
              "File name:" + filename.substring(0, filename.lastIndexOf(".")) ||
              filename;
          },
          false
        );
      }
    </script>
  </body>
</html>

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

0

your issue come from the line

 var fullPath = document.querySelector(".foto").src

that return only the first element that match selector .foto

to solve your issue recover image clicked by :

  1. pass an event parameter to your function
  2. recover target clicked with var target = e.target || e.srcElement;

but instead have 12 eventListeners it's better to have only one on parent element of all your clicked image (in your sample .box but you can create a parent one box-pic in my below sample)

var box = document.querySelector(".box-pic");

box.addEventListener('click', function(e) {
  var target = e.target || e.srcElement;
  if (target.src) {
      var filename = target.src.replace(/^.*[\\\/]/, "");
      document.querySelector(".picName").textContent = "File name:" + filename.substring(0, filename.lastIndexOf(".")) ||
        filename;
  }
});
.box {
  margin: auto;
  width: 200px;
  background-color: lightgray;
}

.box p {
  color: green;
  font-weight: bold;
}
<h1>Selects image file name</h1>
<div class="box">
  <div class="box-pic">
    <img class="foto" src="images/step1.png" alt="img1" />
    <img class="foto" src="images/step2.png" alt="img2" />
    <img class="foto" src="images/step3.png" alt="img3" />
    <img class="foto" src="images/step4.png" alt="img4" />
    <img class="foto" src="images/step5.png" alt="img5" />
    <img class="foto" src="images/step6.png" alt="img6" />
    <img class="foto" src="images/step7.png" alt="img7" />
    <img class="foto" src="images/step8.png" alt="img8" />
    <img class="foto" src="images/step9.png" alt="img9" />
    <img class="foto" src="images/step10.png" alt="img10" />
    <img class="foto" src="images/step11.png" alt="img11" />
    <img class="foto" src="images/step12.png" alt="img12" />
  </div>

  <p class="picName">Name of file:</p>
  <br />
  <hr />
</div>

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