"Navigation Side Menu: w3-sidebar with hamburger toggle feature

If you are looking to implement the w3-sidebar, you can find it here: https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_sidebar_over

The existing code uses a "hanburger" icon to open the side menu and a "Close X" menu item to close it.

To simplify the process, I want to use the same "hanburger" icon for both opening and closing the menu. To keep the hanburger always visible, I have included the following CSS:

.w3-sidebar {
  margin-top: 42px;
}

Here is the snippet of code for reference:

function w3_open() {
  document.getElementById("mySidebar").style.display = "block";
}

function w3_close() {
  document.getElementById("mySidebar").style.display = "none";
}
<!DOCTYPE html>
<html>
  <head>
    <title>W3.CSS</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  </head>
  <body>
    <!-- Sidebar -->
    <div class="w3-sidebar w3-bar-block w3-border-right" style="display:none" id="mySidebar">
      <button onclick="w3_close()" class="w3-bar-item w3-large">Close &times;</button>
      <a href="#" class="w3-bar-item w3-button">Link 1</a>
      <a href="#" class="w3-bar-item w3-button">Link 2</a>
      <a href="#" class="w3-bar-item w3-button">Link 3</a>
    </div>

    <!-- Page Content -->
    <div class="w3-teal">
      <button class="w3-button w3-teal w3-xlarge" onclick="w3_open()">☰</button>
      <div class="w3-container">
        <h1>My Page</h1>
      </div>
    </div>

    <img src="img_car.jpg" alt="Car" style="width:100%">

    <div class="w3-container">
      <p>By default, this sidebar is hidden (style="display:none")</p>
      <p>To reveal it, simply click on the "hamburger" icon in the top left corner.</p>
      <p>The sidebar will cover part of the page's content when opened.</p>
    </div>
  </body>
</html>

Answer №1

A simple solution is to modify the following:

<button class="btn btn-primary btn-lg" onclick="openMenu()">☰</button>

to

<button class="btn btn-primary btn-lg" onclick="toggleMenu()">☰</button>

and insert:

   function toggleMenu() {
        if (document.getElementById("navMenu").style.display == "none") {
            document.getElementById("navMenu").style.display = "block";
        } else { 
            document.getElementById("navMenu").style.display = "none";
        }
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Retrieving Data Using jQuery's .ajax Method in Visual Basic

<WebMethod()> Public Shared Function gtet() As String ... Dim GET = uClass.GetSets(dbuser, dbparam1) ... End Function and $(document).ready(function () { data = { }; var jsondata = $.toJSON(data); $.ajax({ type: "GET ...

Prevent clicks from triggering on dynamically loaded ajax content within a DIV

I am seeking help on how to prevent click events from being triggered on dynamically loaded content within a specific DIV element. <div id="left_column"> <div class="menu">------</div> <div class="menu">------</div> ...

How can I add navigation dots to my slider?

I've been experimenting with my slider and I managed to make it slide automatically. However, the issue is that there is no option to manually navigate through the slides. I am looking to add navigation dots at the bottom so users can easily switch be ...

Is there a way to insert text onto an Isotope image?

I recently created a portfolio using Isotope and Jquery, but I am struggling to add descriptive text to my images without disrupting the layout of the portfolio. I have experimented with various options such as using position:relative for the images and p ...

An error occurred while trying to load the XMLHttpRequest due to a NetworkError that was

I have encountered an issue while working on a client-side project using jQuery, JavaScript, and various jQuery plugins. Our professor supplied us with a proxy.php file to fetch the required data for our web application. I incorporated the easy tab plugin ...

The system does not detect the form

I'm having trouble with my form not being recognized. I've gone over everything multiple times but still can't find the mistake. The PHP code is supposed to take the information from the form and display it, but I keep getting an error that ...

Initiate an "execute.document" command directly from the address bar

While reviewing my old website, I stumbled upon this snippet: <input type="image" id="QuickPass" name="QuickPass" src="/images/QuickPass.gif" alt="Quick Pass" onclick="document.pressed=this.value" value="QuickPass" /> nestled within this form: & ...

fancy box appears when double-clicking on a div element

Help needed: How can I open a div in fancybox on double clicking another div using jQuery with Rails 3.1? Here is the function I have inside document.ready() $("#a1").dblclick(function () { $.get("/projects/edit/"+this.id,{ u:$('#user').val ...

Capturing JSON response in Jquery

I'm struggling to figure out what's going wrong with this code. I can't seem to get any response in the status field... <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"> </script> <input typ ...

Guide to creating HTML file with SSI tags

Currently, my website uses Nginx SSI tags to dynamically include template files such as the header, footer, and sidebar. However, I need to provide the final HTML output to clients instead of running it on their browsers. The issue I'm facing is that ...

I have managed to update the Immutable and Primitive Data-Types in JS. Now, the question arises - are these truly Primitives or are the concepts in

In JavaScript, there are 4 primitive data types that store values directly: String, Number, Boolean, and Symbol. I am excluding undefined and null from this list as they are special data types with unique characteristics. One key feature of primitives is ...

Incomplete DOM elements in jQuery are like puzzles that need to be solved

My issue revolves around jQuery and manipulating DOM elements. I need a specific template setup, as shown below: var threadreply = " <li class='replyItem'>" + " <div class='clearfix'>" + ...

Dynamic HTML element

I created an input number field and I want to dynamically display the value of this field in a container without having to refresh the page. I am currently using ajax for this purpose, but unfortunately, I still need to reload the page. HTML: < ...

What causes the inconsistency in CSS Hover behavior?

Looking to devise a button in css and html that reveals another button below it by rotating on its lowermost axis when hovered over. Progress has been made here: http://codepen.io/machinarius/pen/BdtCb However, there seems to be an issue with the hover b ...

Printing style in CSS

Everything looks good on my webpage. For printing, my CSS adjusts the layout and section sizes. Unfortunately, there is an issue with the print layout - there is a one-inch margin on the left side causing two elements to go off the page on the right. I ...

Guide on navigating to a specific section on a different page using scrolling

Adding a link for a "read more" button is simple. Just include the href attribute like this: <a href="about.html#post2">readmore</a> In my index.html page, I have implemented Bootstrap 3 with scroll animations for navbar sections. When clicki ...

Tips on how to showcase list items in a horizontal manner while maintaining consistent spacing and ensuring they are

I have a list of items denoted by the "li" tag, and I am looking for a way to display them horizontally or in a neat format. Below is the HTML code snippet: <!doctype html> <html lang="en" ng-app="app"> <head> ... </head> <bo ...

Stopping a velocity.js animation once it has completed: is it possible?

For the pulsating effect I'm creating using velocity.js as a fallback for IE9, refer to box2 for CSS animation. If the mouse leaves the box before the animation is complete (wait until pulse expands and then move out), the pulsating element remains vi ...

Implementing AJAX to populate a dropdown list: A step-by-step guide

I have a question about populating options in a select dropdown using AJAX. Can you spot any errors in my code? Below is the snippet from my View: $(function() { $.getJSON("Animes/GetCategories", null, function(data) { var options = '&a ...

Can someone please explain how to apply various fonts to the text in a `<p>` paragraph using CSS and HTML?

Take a look at this image Can you provide guidance on how to incorporate various fonts within a paragraph, like the example shown in the image above? I am experimenting with the "Creative Corner" section. ...