By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Show/hide multiple images individually
JavaScript

Javascript – Show/hide multiple images individually

Admin
Last updated: 2024/02/07 at 6:03 PM
Admin
Share
2 Min Read
Show/hide multiple images individually

Problem:

I’m trying to make a toggle where you can show and hide images. I found an answer from a few years ago that only works with one image. When I add another button and image, all buttons only control the first image. How do I make it so that I can show/hide them all individually?

Contents
Problem:Solution:

Pretty much all other questions ask about several sections showing at once at toggle, but I need them to have individual buttons.

I tried to use image classes but that only broke it completely. The images stopped being hidden when I did that.

Here’s a codepen of what I have:
https://codepen.io/thepasteldyke/pen/vYvwwGo

This is the question where I got the code:
Making a button that shows a hidden image on click
The answer in specific is the one by user Lucas.

function toggleImage(){
  document.querySelector('#image').classList.toggle('hidden');
}
.hidden{
  display: none;
}

.w-100{
  width: 100%;
}

.mb-10{
  margin-bottom: 10px;
}
<button onClick="toggleImage()" class="w-100 mb-10">Show/Hide</button>

<img src="https://images.unsplash.com/photo-1567653418876-5bb0e566e1c2?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1964&q=80" id="image" class="hidden w-100"/>

<button onClick="toggleImage()" class="w-100 mb-10">Show/Hide</button>

<img src="https://images.unsplash.com/photo-1520052205864-92d242b3a76b?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8OXx8cGlua3xlbnwwfHwwfHx8MA%3D%3D&auto=format&fit=crop&w=500&q=60" id="image" class="hidden w-100"/>

Solution:

The issue you are encountering stems from using the same ID for multiple elements. In HTML, IDs should be unique, meaning each ID can only be assigned to one element. When you use document.querySelector(‘#image’), it only selects the first element with that specific ID, which is why only the first image is affected.

<!DOCTYPE html>
<html>
<head>
<style>

    .hidden{
      display: none;  
    }

    .w-100{
      width: 100%;
    }

    .mb-10{
      margin-bottom: 10px;
    }


</style>


<script>

    function toggleImage(imageId){
      document.querySelector('#'+imageId).classList.toggle('hidden');
    }

</script>

</head>
<body>

    <button onClick="toggleImage('image1')" class="w-100 mb-10">Show/Hide</button>

    <img src="https://images.unsplash.com/photo-1567653418876-5bb0e566e1c2?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1964&q=80" id="image1" class="hidden w-100"/>

    <button onClick="toggleImage('image2')" class="w-100 mb-10">Show/Hide</button>

    <img src="https://images.unsplash.com/photo-1520052205864-92d242b3a76b?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8OXx8cGlua3xlbnwwfHwwfHx8MA%3D%3D&auto=format&fit=crop&w=500&q=60" id="image2" class="hidden w-100"/>

</body>
</html>

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to filter an array of objects using another array of objects? Javascript – How to filter an array of objects using another array of objects?
Next Article How to intercept an eventhandler in javascript? Javascript – How to intercept an eventhandler in javascript?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?