I'm trying to create a dynamic webpage where each button displays a specific <div>
page while hiding the rest of the <div>
pages.
I deploy it on here, in my localhost, and on jsfiddle but somehow it's not operating correctly on jsfiddle and my localhost BUT it works on here.
I'm not quite sure why is that so?
My HTML:
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction()">Try it</button>
<button onClick="myFunction2()">Try it</button>
<button onClick="myFunction3()">Try it</button>
<div id="myDIV">
This is my DIV element.
</div>
<div id="div2" style="display: none;">
Div2
</div>
<div id="div3" style="display: none;">Div3</div>
My CSS:
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
}
#div2{
width: 100%;
padding: 50px 0;
text-align: center;
background-color: purple;
margin-top: 20px;
}
#div3{
width: 100%;
padding: 50px 0;
text-align: center;
background-color: yellow;
margin-top: 20px;
}
My JavaScript:
function myFunction() {
var x = document.getElementById("myDIV");
var y = document.getElementById("div2");
var z = document.getElementById("div3");
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "none";
}
}
function myFunction2() {
var x = document.getElementById("div2");
var y = document.getElementById("myDIV");
var z = document.getElementById("div3");
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "none";
}
}
function myFunction3() {
var x = document.getElementById("div3");
var y = document.getElementById("myDIV");
var z = document.getElementById("div2");
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "none";
}
}
function myFunction() {
var x = document.getElementById("myDIV");
var y = document.getElementById("div2");
var z = document.getElementById("div3");
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "none";
}
}
function myFunction2() {
var x = document.getElementById("div2");
var y = document.getElementById("myDIV");
var z = document.getElementById("div3");
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "none";
}
}
function myFunction3() {
var x = document.getElementById("div3");
var y = document.getElementById("myDIV");
var z = document.getElementById("div2");
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "none";
}
}
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
}
#div2{
width: 100%;
padding: 50px 0;
text-align: center;
background-color: purple;
margin-top: 20px;
}
#div3{
width: 100%;
padding: 50px 0;
text-align: center;
background-color: yellow;
margin-top: 20px;
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction()">Try it</button>
<button onClick="myFunction2()">Try it</button>
<button onClick="myFunction3()">Try it</button>
<div id="myDIV">
This is my DIV element.
</div>
<div id="div2" style="display: none;">
Div2
</div>
<div id="div3" style="display: none;">Div3</div>
EDIT:
My JSfiddle link: https://jsfiddle.net/yvw4gwwh/3/