Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!