Hello I am a relative rookie when it comes to web design in general and am having a heck of a time trying to figure out how to toggle a png image from hidden to visible. Effectively what I want to happen is to have a base image that is always displayed on the webpage and be able to use checkboxes to toggle on or off specific png images. I am sure that it is a very simple thing to do but after spending a half a day searching Google I thought better of myself and decided to ask for some help.
Here is the HTML, CSS, and JavaScript that I have:
<!DOCTYPE html>
<html>
<head>
<!---->
<style>
.buttonBar {
width:64%;
margin: 2% 17%;
padding: 1%;
border: 2px solid #44506e;
border-radius: 5px;
background-color: #c1baa9;
}
.pngCake {
position: relative;
width:60%;
margin-left: 17%;
padding: 3%;
border: 2px solid #44506e;
border-radius: 5px;
background-color: #c1baa9;
}
#base {
position: relative;
top: 0;
left: 0;
}
.pngCake #png, .pngCake #png1 {
position: absolute;
top: 0;
left: 0;
visibility: hidden;
}
</style>
<title>Image Cake</title>
</head>
<body>
<div class="buttonBar">
<label class="cb">
<input type="checkbox" onclick="pngLayer()">
<span class="cm"></span>
PNG</label>
<!-- Printer Checkbox -->
<label class="cb">
<input type="checkbox" onclick="pngLayer1()">
<span class="cm"></span>
PNG 1</label>
</div>
<div class="pngCake">
<img id="base" src="base.png"></img>
<img id="png" src="png.png"></img>
<img id="png1" src="png1.png">
</div>
<script>
function pngLayer() {
var portCheck = document.getElementById("png");
portCheck.style.visibility = "visible";
}
function pngLayer1() {
var portCheck = document.getElementById("png1");
portCheck.style.visibility = "visible";
}
</script>
</body>
</html>
In JavaScript I have tried using a toggle command to pull a duplicate of the CSS with the visibility set to visible.
In addition I have tried using if else statements to check and see if the visibility of the png is set to hidden vs visible and changing it.
In each case the result did not work.
if(document.getelementbyid("png").style.visibility == "hidden") {
document.getelementbyid("png").style.visibility == "visible"
}
else {
document.getelementbyid("png").style.visibility == "hidden
}
if (document.getelementbyid("png").style.visibility == "hidden") {
document.getelementbyid("png").style.visibility == "visible"
} else {
document.getelementbyid("png").style.visibility == "hidden"
}
function pngLayer() {
var portCheck = document.getElementById("png");
portCheck.style.visibility = "visible";
}
function pngLayer1() {
var portCheck = document.getElementById("png1");
portCheck.style.visibility = "visible";
}
.buttonBar {
width: 64%;
margin: 2% 17%;
padding: 1%;
border: 2px solid #44506e;
border-radius: 5px;
background-color: #c1baa9;
}
.pngCake {
position: relative;
width: 60%;
margin-left: 17%;
padding: 3%;
border: 2px solid #44506e;
border-radius: 5px;
background-color: #c1baa9;
}
#base {
position: relative;
top: 0;
left: 0;
}
.pngCake #png,
.pngCake #png1 {
position: absolute;
top: 0;
left: 0;
visibility: hidden;
}
<div class="buttonBar">
<label class="cb">
<input type="checkbox" onclick="pngLayer()">
<span class="cm"></span>
PNG</label>
<!-- Printer Checkbox -->
<label class="cb">
<input type="checkbox" onclick="pngLayer1()">
<span class="cm"></span>
PNG 1</label>
</div>
<div class="pngCake">
<img id="base" src="base.png"></img>
<img id="png" src="png.png"></img>
<img id="png1" src="png1.png">
</div>
3
Answers
See these, the comments in the code explain how it works.
(I have only copied what I have changed)
event.target.checked
– returns either true (checked) or false (unchecked) depending on the state of the checkbox.Ternary Operator Docs
Hope this helps.
I had a look at your script but it’s an absolute mess so it would take far less time creating such a simple program from scratch.
You only need one image tag and to just change the src attribute as the user clicks on the selection control.
The image selection control is best represented by radio buttons that only allow one choice at a time as there’s no reason in having three images stacked up on each other.
Ok, here’s the script… replace the image paths where I have indicated in the comments and keep practicing.
CodePen
In this CodePen it’s what I think could be a good way of solving this, please give a good read before trying it out to see if it matches your use case.
The HTML part is pretty much whatever you need to be, I just placed a wrapper, so I could style them better for the test.
In JS part,
There’s an array of strings, where I map though them to build my
imgs
This is where you could place your URL’s.
Right under it, I map the
images
and createimg
andcheckbox
(if you need to create the checkbox elsewhere, you can just create a different function to append it differently than the one I have)And then there’s the
onToggle
function, where I get theimg
by itsid
, (that I set when I was creating it) and then check if the classhidden
is on the class list, if it’s not, I add it, otherwise I remove it, toggling the image visibility.And the CSS
I did it this way, but honestly you could use
inline styles
if you prefer to.Thinking as developer, since you are loading multiple images, I would try to minimize adding them manually to just looping though them.
But you could do this manually too, invoking the function
onToggle
and passing theid
in eachcheckbox
onclick
. Like so: