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

177
Views
Gallery is not interactive while using JavaScript and Css

I have built a gallery that supposes to change the main picture when other one is chosen from the small menu down below, however, it does not react when i click on other small photos it just stays the same, could anyone please help with it

// image gallery
const changeimage = (src) => {
  document.getElementById("main").src = src
}
.gallery {
  width: 90%;
  margin: 0 auto;
}

.gallery-main {
  width: 45rem;
  height: 30rem;
  margin: 0 auto;
}

.gallery-main img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}

.gallery-p {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 2rem;
}

.gallery-p img {
  width: 9rem;
  height: 7rem;
  object-fit: cover;
  border-radius: 4px;
  margin: 0.5rem;
  cursor: pointer;
}
<div class="gallery">
  <div class="gallery-main">
    <img src="images/dublin culture1.jpg" alt="" id="main">
  </div>

  <div class="gallery-p">
    <img src="images/dublin culture2.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin culture3.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin culture4.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin people1.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin people2.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin people3.jpg" alt="" onclick="changeimage(this.src)">
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this!

document.getElementById("main").src = "https://i.ibb.co/2kfJ15Z/image-cropped.jpg"

const changeimage = (src) => {
    document.getElementById("main").src = src
}
.gallery {
  width: 90%;
  margin: 0 auto;
}

.gallery-main {
  width: 45rem;
  height: 30rem;
  margin: 0 auto;
}

.gallery-main img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}

.gallery-p {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 2rem;
}

.gallery-p img {
  width: 9rem;
  height: 7rem;
  object-fit: cover;
  border-radius: 4px;
  margin: 0.5rem;
  cursor: pointer;
}
<div class="gallery">
  <div class="gallery-main">
    <img src="images/dublin culture1.jpg" alt="" id="main">
  </div>

  <div class="gallery-p">
    <img src="https://i.ibb.co/2kfJ15Z/image-cropped.jpg" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/0GwkygJ/imagename-2.png" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/2kfJ15Z/image-cropped.jpg" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/0GwkygJ/imagename-2.png" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/2kfJ15Z/image-cropped.jpg" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/0GwkygJ/imagename-2.png" alt="" onclick="changeimage(this.src)">
  </div>
</div>

Thanks and best regards!

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!