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

356
Views
How can I change an image when I click on another image? (Without onclick)

I´m doing a landing page and wanted to make the main image change when I click on other images. I´ve tried with this

<div class="thumb">
  <ul>
    <li> 
      <img id="img1" class="smallImg coupe" src="./assets/smallCar1.png" alt="small img">
    </li>
  </ul>
  <ul>
    <li>
      <img id="img2" class="smallImg" src="./assets/smallCar2.png" alt="small img">
    </li>
  </ul>
  <ul>
    <li>
      <img id="img3" class="smallImg" src="./assets/smallCar3.png" alt="small img">
    </li>
  </ul>
</div>
function imageChange() {
  let picDefault = document.getElementById("img");

  if ((picDefault = "img")) {
    picDefault.src = "./assets/img1.png";
  }
}
picDefault.addEventListener(click, imageChange);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sample code for updating an image by clicking on another image. Use case might be completely different.

const thumps =  document.getElementsByClassName("thump"); // secondary images
const main =  document.getElementById("main");

for(let i = 0; i < thumps.length; i ++) {
  const el = thumps[i]
  el.addEventListener("click", function(e) { //  add click handler
    const elem = e.target;
    main.src = elem.src;
  })
}
#main {
  max-width: 300px;
}

.thump {
  width: 100px;
  margin: 4px;
  padding: 2px;
  border: 1px solid #ccc;
  display: inline-block;
}
<div>
  <h5>Main Image</h5>
  <img src="https://images.unsplash.com/photo-1644243019151-0bb97ce1af84" id="main"/>
</div>

<h5>Click Images below thumpnails to update main image</h5>
<div class="row">
  <img src="https://images.unsplash.com/photo-1644235279538-4cc7cbdca6a8" class="thump">
  <img src="https://images.unsplash.com/photo-1643443026948-c17b9bb16758" class="thump">
  <img src="https://images.unsplash.com/photo-1644243019151-0bb97ce1af84" class="thump">
</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!