I'm trying to create a button that does two things when clicked. The first, toggle between a hamburger menu icon and a times icon (X). The second action is to change a side navigation element's width from 0px to 250px and then back to 0px if clicked again (syncing up with the hamburger menu switching from and X and back to a hamburger). I've been able to get various parts of these actions to work, but haven't been able to bring them all together. Here are the functions I currently have:
<script>
document.getElementById("test").addEventListener("click", myFunctionTwo);
function myFunctionTwo() {
document.getElementById("mySidenav").style.width = "250px";
}
function myFunction(x) {
x.classList.toggle("fa-times");
x.classList.toggle("fa-bars");
}
</script>
HTML:
<button id="test">
<i onclick="myFunction(this)" class="fa fa-bars"></i>
</button>
<div id="mySidenav" class="sidenav">
<a href="#">NFL</a>
<a href="#">NBA</a>
<a href="#">MLB</a>
</div>
Something tells me that my onclick function for the i element should be combined with the event listener for my button but I cannot get it to work properly. I also need to add a way to hide the sidenav back to a 0px width if the button is clicked again.
What is the proper way to go about doing something like this?
Thanks in advance for the help!