I have 4 headers on my website and two sections. Each section is having 4 images and each image has an id. By default, only the images with the id defaultimg
are visible in both sections, the rest of every image is hidden.
What I want to do-
I want to show only the requested image in both sections. For example, if click on the first header which is saying, “Click Here To Show IMG 1” the only image with the id “exp1img” should show in both sections, and the rest should hide. The same process goes for all 4 headers.
Where I am having trouble-
When I click on any header element, only the first section’s images are toggling perfectly (hide and visible according to the request). There are no updates in the second section.
Extra requirement-
I want to implement the solution using for loop but because of new to JS, I need guidance to do this.
Below is my code-
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <h3 onclick="ShowImg1();">Click Here To Show IMG 1</h3> <h3 onclick="ShowImg2();">Click Here To Show IMG 2</h3> <h3 onclick="ShowImg3();">Click Here To Show IMG 3</h3> <h3 onclick="ShowImg4();">Click Here To Show IMG 4</h3> <!-- img section n#1 --> <span id="defaultimg"><p><img alt="deffault-img" src="https://i.ibb.co/kqjt7wM/default-img.png"></p><br></span> <span id="exp1img" style="display: none;"><p><img alt="img1" src="https://i.ibb.co/wcdx6pt/img1.png"></p><br></span> <span id="exp2img" style="display: none;"><p><img alt="img2" src="https://i.ibb.co/XpFmqGT/img2.png"></p><br></span> <span id="exp3img" style="display: none;"><p><img alt="img3" src="https://i.ibb.co/yp0jvKS/img3.png"></p><br></span> <span id="exp4img" style="display: none;"><p><img alt="img4" src="https://i.ibb.co/6bYs2Jh/img4.png"></p><br></span> <!-- img section n#2 --> <span id="defaultimg"><p><img alt="deffault-img" src="https://i.ibb.co/kqjt7wM/default-img.png"></p><br></span> <span id="exp1img" style="display: none;"><p><img alt="img1" src="https://i.ibb.co/wcdx6pt/img1.png"></p><br></span> <span id="exp2img" style="display: none;"><p><img alt="img2" src="https://i.ibb.co/XpFmqGT/img2.png"></p><br></span> <span id="exp3img" style="display: none;"><p><img alt="img3" src="https://i.ibb.co/yp0jvKS/img3.png"></p><br></span> <span id="exp4img" style="display: none;"><p><img alt="img4" src="https://i.ibb.co/6bYs2Jh/img4.png"></p><br></span> </body> <script> function ShowImg1() { const visibility = "block"; const novisibility = "none"; document.getElementById("exp1img").style.display=visibility ; document.getElementById("defaultimg").style.display=novisibility ; document.getElementById("exp2img").style.display=novisibility ; document.getElementById("exp3img").style.display=novisibility ; document.getElementById("exp4img").style.display=novisibility ; } function ShowImg2() { const visibility = "block"; const novisibility = "none"; document.getElementById("exp2img").style.display=visibility ; document.getElementById("defaultimg").style.display=novisibility ; document.getElementById("exp1img").style.display=novisibility ; document.getElementById("exp3img").style.display=novisibility ; document.getElementById("exp4img").style.display=novisibility ; } function ShowImg3() { const visibility = "block"; const novisibility = "none"; document.getElementById("exp3img").style.display=visibility ; document.getElementById("defaultimg").style.display=novisibility ; document.getElementById("exp2img").style.display=novisibility ; document.getElementById("exp1img").style.display=novisibility ; document.getElementById("exp4img").style.display=novisibility ; } function ShowImg4() { const visibility = "block"; const novisibility = "none"; document.getElementById("exp4img").style.display=visibility ; document.getElementById("defaultimg").style.display=novisibility ; document.getElementById("exp2img").style.display=novisibility ; document.getElementById("exp3img").style.display=novisibility ; document.getElementById("exp1img").style.display=novisibility ; } </script> </html>
Advertisement
Answer
The problem is that you are using the same id for multiple elements which is not correct. You should use class names instead.
Below is a working solution to your problem using for loop and switch cases. Here is what I did-
I created a common function
ShowImg
and pass it a number that we would like to display the image of the number.If we pass number 1 that means all images which have class
exp1img
should be visible, so these images will come undershowCategory
, rest of the images will come underhideCategories
.Now, I created a function named
changeImgStatus
to change the image’s status (visible or none). This function will accept the array of images and status to change.
So what this code is doing-
When we click on the h3
element, it will call the function ShowImg
and pass a number to it. Then, the ShowImg
function will check the number and using the switch case decides which images to hide and which to show.
then finally, it will call another function changeImgStatus
, and pass it to the showCategory
and hideCategories
images to change their status.
In that way you don’t need to create multiple functions to show and hide images, only single function can do all of this.
const visibility = "block"; const novisibility = "none"; let defaultImages = document.querySelectorAll('.defaultimg'); let exp1Images = document.querySelectorAll('.exp1img'); let exp2Images = document.querySelectorAll('.exp2img'); let exp3Images = document.querySelectorAll('.exp3img'); let exp4Images = document.querySelectorAll('.exp4img'); function changeImgStatus(arr, status) { arr.forEach(el => { el.style.display = status; }) } function ShowImg(number) { let showCategory; let hideCategories; switch(number) { // If want to display only type 1 images. case 1: showCategory = exp1Images; hideCategories = [defaultImages, exp2Images, exp3Images, exp4Images]; break; // If want to display only type 2 images. case 2: showCategory = exp2Images; hideCategories = [defaultImages, exp1Images, exp3Images, exp4Images]; break; // If want to display only type 3 images. case 3: showCategory = exp3Images; hideCategories = [defaultImages, exp1Images, exp2Images, exp4Images]; break; // If want to display only type 4 images. case 4: showCategory = exp4Images; hideCategories = [defaultImages, exp1Images, exp2Images, exp3Images]; break default: showCategory = defaultImages; hideCategories = [exp1Images, exp2Images, exp3Images, exp4Images]; } // change the status to visible of showCategory images. changeImgStatus(showCategory, visibility); // change the status to non visible of hide category images. hideCategories.forEach(category => { changeImgStatus(category, novisibility) }) }
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <h3 onclick="ShowImg(1);">Click Here To Show IMG 1</h3> <h3 onclick="ShowImg(2);">Click Here To Show IMG 2</h3> <h3 onclick="ShowImg(3);">Click Here To Show IMG 3</h3> <h3 onclick="ShowImg(4);">Click Here To Show IMG 4</h3> <!-- img section n#1 --> <span class="defaultimg"><p><img alt="deffault-img" src="https://i.ibb.co/kqjt7wM/default-img.png"></p><br></span> <span class="exp1img" style="display: none;"><p><img alt="img1" src="https://i.ibb.co/wcdx6pt/img1.png"></p><br></span> <span class="exp2img" style="display: none;"><p><img alt="img2" src="https://i.ibb.co/XpFmqGT/img2.png"></p><br></span> <span class="exp3img" style="display: none;"><p><img alt="img3" src="https://i.ibb.co/yp0jvKS/img3.png"></p><br></span> <span class="exp4img" style="display: none;"><p><img alt="img4" src="https://i.ibb.co/6bYs2Jh/img4.png"></p><br></span> <!-- img section n#2 --> <span class="defaultimg"><p><img alt="deffault-img" src="https://i.ibb.co/kqjt7wM/default-img.png"></p><br></span> <span class="exp1img" style="display: none;"><p><img alt="img1" src="https://i.ibb.co/wcdx6pt/img1.png"></p><br></span> <span class="exp2img" style="display: none;"><p><img alt="img2" src="https://i.ibb.co/XpFmqGT/img2.png"></p><br></span> <span class="exp3img" style="display: none;"><p><img alt="img3" src="https://i.ibb.co/yp0jvKS/img3.png"></p><br></span> <span class="exp4img" style="display: none;"><p><img alt="img4" src="https://i.ibb.co/6bYs2Jh/img4.png"></p><br></span> </body> </html>