In my current setup, I have 3 buttons that can toggle between different visible divs. In the future, I may add more buttons to switch between additional divs.
At the moment, the first div is active (display set to block), while all other divs are hidden (display set to none).
When a button is clicked, the active div changes based on the button that was clicked.
function switchUnit1() {
if (document.getElementById('units2')) {
if (document.getElementById('units2').style.display == 'block') {
document.getElementById('units2').style.display = 'none';
document.getElementById('units1').style.display = 'block';
}
}
if (document.getElementById('units3')) {
if (document.getElementById('units3').style.display == 'block') {
document.getElementById('units3').style.display = 'none';
document.getElementById('units1').style.display = 'block';
}
}
if (document.getElementById('units4')) {
if (document.getElementById('units4').style.display == 'block') {
document.getElementById('units4').style.display = 'none';
document.getElementById('units1').style.display = 'block';
}
}
}
function switchUnit2() {
if (document.getElementById('units1')) {
if (document.getElementById('units1').style.display == 'block') {
document.getElementById('units1').style.display = 'none';
document.getElementById('units2').style.display = 'block';
}
}
if (document.getElementById('units3')) {
if (document.getElementById('units3').style.display == 'block') {
document.getElementById('units3').style.display = 'none';
document.getElementById('units2').style.display = 'block';
}
}
if (document.getElementById('units4')) {
if (document.getElementById('units4').style.display == 'block') {
document.getElementById('units4').style.display = 'none';
document.getElementById('units2').style.display = 'block';
}
}
}
function switchUnit3() {
if (document.getElementById('units1')) {
if (document.getElementById('units1').style.display == 'block') {
document.getElementById('units1').style.display = 'none';
document.getElementById('units3').style.display = 'block';
}
}
if (document.getElementById('units2')) {
if (document.getElementById('units2').style.display == 'block') {
document.getElementById('units2').style.display = 'none';
document.getElementById('units3').style.display = 'block';
}
}
if (document.getElementById('units4')) {
if (document.getElementById('units4').style.display == 'block') {
document.getElementById('units4').style.display = 'none';
document.getElementById('units3').style.display = 'block';
}
}
}
<div class='units-buttons' id='units-buttons'>
<button class='units-button units-selected' onclick='switchUnit1();' type='button' value='Click'>Type 36A</button>
<button class='units-button' onclick='switchUnit2();' type='button' value='Click'>Type 36B</button>
<button class='units-button' onclick='switchUnit3();' type='button' value='Click'>Type 56</button>
<div id='units1' style='display: block;'>Image 1</div>
<div id='units2' style='display: none;'>Image 2</div>
<div id='units3' style='display: none;'>Image 3</div>
I am looking to incorporate a fading transition effect when switching from one div to another each time a button is clicked.