I am trying to use a button to expand my sidenav by toggling the class with JQuery.
Although I am more familiar with JavaScript, I attempted to solve it with JS before moving to JQuery.
function toggleMenu() {
var sideEle = document.getElementById("sidebar-wrapper");
var mainEle = document.getElementById("page-content-wrapper");
sideEle.classList.toggle("sideMenuDisplayed");
mainEle.classList.toggle("bodyMenuDisplayed");
};
I added onclick="toggleMenu()"
to the button html, but it did not work. I also added borders to both the before and after to check for class changes while debugging.
Here is my current code using JQuery
$(document).ready(function() {
$('#menu-toggle').on('click', function() {
$('#sidebar-wrapper').toggleClass('sideMenuDisplayed');
});
#sidebar-wrapper {
z-index: 1;
position: absolute;
width: 0;
height: 100%;
overflow-y: hidden;
background: #f07878;
border: 2px solid red;
opacity: 0.9;
}
.sideMenuDisplayed {
width: 250px;
border: 2px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="wrapper">
<!-- Sidenav -->
<div id="sidebar-wrapper">
<ul class="sadebar-nav">
<li><a href="/account">Account</a></li>
<li><a href="/blog">blog</a></li>
<li><a href="/contact">Contact me!</a></li>
</ul>
</div>
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="col-1">
<button class="btn" id="menu-toggle"><i class="fas fa-bars fa-2x" id="homeIcon"></i></button>
</div>
<div class="container" id="homeContainer">
<p>my content</p>
</div>
</div>
When I press the button, there are no errors in the console, and the page does not change. The button should toggle the class of #sidebar-wrapper
and change its properties to width: 250px;
and border color to blue