I’ve been struggling with this for a while, and googling outrageously has brought me thus far.
I’ve generated a page, and despite my script, I can’t get the sidebar to disappear.
See the following
html {
margin-left: 0;
margin-top: 0;
}
table,
th,
td {
border: 1px solid;
}
#body {
min-height: 100vh;
display: grid;
grid-template-rows: auto 1fr auto;
grid-template-columns: 300px auto;
grid-template-areas: 'menubtn header ' 'sidebar content' 'footer footer';
}
#menubtn {
grid-area: menubtn;
position: sticky;
top: 0;
height: 3rem;
}
#header {
grid-area: header;
background-color: white;
position: sticky;
top: 0;
height: 3rem;
}
#sidebar {
grid-area: sidebar;
background-color: white;
position: sticky;
height: calc(100vh -3rem);
align-self: start;
}
#sidebar.hidden {
display: hidden;
}
content {
grid-area: content;
}
#footer {
grid-area: footer;
background-color: white;
}
h1 {
color: #330099;
}
h2 {
color: #330099;
}
h3 {
color: #330099;
}
a:link,
a:visited {
color: #FFF;
font-weight: bold;
background-color: #09F;
border: 1px solid #0066cc;
padding: 3px;
text-decoration: none
}
#content a:link {
color: #FFF;
font-weight: bold;
background-color: #09F;
border: 1px solid #0066cc;
padding: 1px;
text-decoration: none
}
#content a:visited {
color: #FFF;
font-weight: bold;
background-color: #09F;
border: 1px solid #0066cc;
padding: 1px;
text-decoration: none
}
a:hover {
background-color: #06C
}
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="wvv_grid.css" type="text/css">
<title>
Test page for toggling menu
</title>
</head>
<body>
<div id="body">
<div id="menubtn">
<button id="Button" onclick="myFunc()">Toggle menu</button> </div>
<div id="header">
<h1>Welcome to my test page</h1>
</div>
<div id="sidebar" ,>
<ul>
<li>this</li>
<li>item</li>
<li>tests</li>
<li>the</li>
<li>button</li>
</ul>
</div>
<div id="content">
<h2>welcome again!</h2>
<p>this page has been created to test my button to toggle menus. I've put another sentence here just to check whether or not the content area is expanded when the menu is hidden.</p>
</div>
</div>
<script defer="true">
function myFunc() {
let sb = document.getElementById("#sidebar");
sb.classList.toggle("hidden");
}
</script>
</body>
</html>
I’m confused over a couple of things which may help point any help in the right direction. Not sure if my hidden class is correctly defined. Further to this, you’ll notice I’ve not used semantic tags. what do I need to change to make this happen?
Many thanks.
2
Answers
#
when usingdocument.querySelector("#sidebar")
, not when usingdocument.getElementById("sidebar")
<< no#
#sidebar.hidden {display: hidden;}
should bedisplay: none;
on*
handlers. UseaddEventListener()
instead like i.e:document.querySelector("#Button").addEventListener("click", toggleSidebar);
sb
, such might add a huge cognitive load when your code becomes largercontent {
should be#content {
<div id="sidebar" ,>
should be<div id="sidebar">
There are lots of ways you can achieve this effect.
Here is just one example.
In the example I have applied the CSS transition not only to the sidebar (the
<aside>
element) but also to the<main>
element, so that the content area is expanded when the menu is hidden.Working Example: