For a recent project, I was tasked with creating a toggle-style button setup for the challenge on CodePen. To achieve a 3D appearance for the button, I wrote some JavaScript code that utilized the onclick event.
var on = true
const green = document.getElementById('on')
const red = document.getElementById('off')
var greenOn = function() {
if (on == false) {
green.style.boxShadow = 'inset -3px 3px 13px 0px rgba(0,0,0,0.15) inset'
red.style.boxShadow = 'none'
var on = true;
}
}
var redOn = function() {
if (on == true) {
green.style.boxShadow = 'none'
red.style.boxShadow = '-3px 3px 13px 0px rgba(0,0,0,0.15) inset'
var on = false
}
}
Here's the neatly bundled code snippet for your reference.
var on = true
const green = document.getElementById('on')
const red = document.getElementById('off')
var greenOn = function() {
if (on == false) {
green.style.boxShadow = 'inset -3px 3px 13px 0px rgba(0,0,0,0.15) inset'
red.style.boxShadow = 'none'
var on = true;
}
}
var redOn = function() {
if (on == true) {
green.style.boxShadow = 'none'
red.style.boxShadow = '-3px 3px 13px 0px rgba(0,0,0,0.15) inset'
var on = false
}
}
.on {
border-radius: 5px;
background-color: lime;
padding: 50px;
border: none;
}
.off {
border-radius: 5px;
background-color: red;
padding: 50px;
border: none;
}
.switch {
padding: 0;
margin: 0;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="switch">
<button class="on" id="on" onclick="greenOn()"></button>
<button class="off" onclick="redOn()" id="off"></button>
</div>
</body>
</html>
However, upon testing the implementation, nothing seems to happen when clicking the buttons. Any suggestions on how to fix this issue would be greatly appreciated!