Salman Salman - 1 month ago 6
CSS Question

Why cant I get the divs to change onclick?

I'm tryin to make a very simple website and I just want the two divs to change as I click on the corresponding list button on top.But the second div never shows up when I click on the second list button. Can't figure out why for hours now, all steps in the code seem logical. I want to do this without Jquery or anything else.



window.onload = function() {
d1 = document.getElementById('myDiv1');
d2 = document.getElementById('myDiv2');
function showDiv1() {
if( d2.style.display == "block" )
{
d2.style.display = "none";
d1.style.display = "block";
}
else
{
return false;
}
}

function showDiv2() {
if( d1.style.display == "block" )
{
d1.style.display = "none";
d2.style.display = "block";
}
else
{
return false;
}
}
}

body {
margin: 0;
padding: 0;
background-repeat: no-repeat;
background-position: center;
text-align: center;
font-size: 100%;
height: 100%;
}

.myDiv1, .myDiv2 {
background-image: url("paper.jpg");
background-color: none;
margin: auto;
height: 80%;
border: 2px solid black;
border-radius: 0px;
text-align: left;
/*color: white;
font-family: "Arial", Times, serif;
font-weight: bold;*/
padding: 10px;
overflow: auto
}

.myDiv1 {
display: block;
}

.myDiv2 {
display: none;
}

p {

}


.ul1 {
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
background-color: #dddddd;
height: 70px;
}
.li1 {
float: right;
}
.li1 a {
display: block;
color: black;
text-decoration: none;
padding: 25px
}
.li1:first-child {
margin-right: 100px;
}

/* Grow */
.hvr-grow {
display: inline-block;
vertical-align: middle;
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
backface-visibility: hidden;
-moz-osx-font-smoothing: grayscale;
transition-duration: 0.3s;
transition-property: transform;
}

.hvr-grow:hover,
.hvr-grow:focus,
.hvr-grow:active {
transform: scale(1.1);
}

.ul2 {
list-style-type: none;
margin: 20px auto;
padding: 0px;
overflow: hidden;
background-color: black;
border: 1px solid black;
border-radius: 20px;
width: auto;
display: flex;
}
.li2 {
display: inline;
border-right: 1px solid white;
width: 25%
}
.li2:last-child {
border-right: none;
}
.li2 a {
color: white;
text-decoration: none;
}

.ul3 {
list-style-type: none;
margin: 0;
padding: 0;
}
.li3 {
margin: 15px 0;
}

#myDiv1:checked ~ #myDiv .inner { margin-left:0; }
#myDiv2:checked ~ #myDiv .inner { margin-left:-100%; }
#myDiv3:checked ~ #myDiv .inner { margin-left:-200%; }

<ul class="ul1">
<li class="li1 hvr-grow" ><a href="#sport" onclick="showDiv1()">SPORT</a>
</li>
<li class="li1 hvr-grow"><a href="#sportler" onclick="showDiv2()">SPORTLER</a>
</li>
<li class="lix">Meine Webseite</li>
</ul>


<div class="myDiv1" id="myDiv1">
<ul class="ul3">
<li class="li3"><a href="default.asp">Icon1</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="news.asp">Icon2</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="contact.asp">Icon3</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon4</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon5</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon6</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon7</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon8</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon9</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon10</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon11</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon12</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon13</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon14</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon15</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon16</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon17</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon18</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon19</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
<li class="li3"><a href="about.asp">Icon20</a></li>
<p style="font-size: 0.85em;">This is the description of this sport</p>
</ul>
</div>
<div class="myDiv2" id="myDiv2">my name</div>

<ul class="ul2">
<li class="li2"><a href="#salman">Salman</a>
</li>
<li class="li2"><a href="#Patric">Patric</a>
</li>
<li class="li2"><a href="#IMIBE">IMIBE</a>
</li>
<li class="li2"><a href="#CUE">CUE</a>
</li>
</ul>




Answer

The double equal to sign '==' in the functions was the problem. It should just be a single '='. Now it works even without using any of the above suggestions. Thanks everyone :)

Comments