Missing sidebar display

Hi there! I am having an issue with my sidebar not appearing correctly when I click on the toggle button. It seems to be moving to the side, but the sidebar itself is blank or transparent. I suspect that the problem lies within my JavaScript file. As a beginner in coding and this community, I would really appreciate some assistance. Thank you in advance!

/* Set the width of the sidebar to 250px and the left margin of the page content to 250px */
function openNav() {
    document.getElementById("mysidebar").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
}


/* Set the width of the sidebar to 0 and the left margin of the page content to 0 */
function closeNav() {
    document.getElementById("mysidebar").style.width = "0";
    document.getElementById("main").style.marginLeft = "0";
}
<body>

    <section id="mysidebar">
        <div class="sidebar">
            <ul>
                <li><a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a></li>
                <li><a href="#">Chapter 1</a></li>
                <li><a href="#">Chapter 2</a></li>
                <li><a href="#">Chapter 3</a></li>
                <li><a href="#">Chapter 4</a></li>
                <li><a href="#">Chapter 5</a></li>
            </ul>
        </div>

        <div id="main">
            <button class="openbtn" onclick="openNav()">&#9776;</button>
            <h2>Collapsed Sidebar</h2>        
        </div>
    </section>

    <script src="C:\Users\quinc\Documents\Developer\Project 2.0\js\members.js"></script>
</body>
</html>

I believe the issue might be related to my JS script. However, I am struggling to identify the exact problem.

If anyone could lend a hand, it would be greatly appreciated. This is my first project and I am new to coding. Below, I have provided my HTML, CSS, and JS code.

Thank you

Answer №1

Upon entering the main section mysidebar, it is noted that the width: 0 setting of the mysidebar section will only be triggered when the closeNav function is clicked.

However, simply adjusting the width to zero may not suffice due to the presence of the ul element within the sidebar division along with text within ul li a also occupying space. It is advised to utilize the css display property for a comprehensive solution.

The recommendation is to modify both the openNav and closeNav functions accordingly:

function openNav() {
    var sidebar = document.getElementById("sidebar");
    sidebar.style.width = "250px";
    sidebar.style.display = "block";
    document.getElementById("main").style.marginLeft = "250px";
}


/* Set the width of the sidebar to 0 and the left margin of the page content to 0 */
function closeNav() {
    document.getElementById("sidebar").style.display = "none";
    document.getElementById("main").style.marginLeft = "0";
}

Furthermore, ensure to update the html code by assigning an id attribute to the sidebar:

<div id="sidebar">
            <ul>
                <li><a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a></li>
                <li><a href="#">Chapter 1</a></li>
                <li><a href="#">Chapter 2</a></li>
                <li><a href="#">Chapter 3</a></li>
                <li><a href="#">Chapter 4</a></li>
                <li><a href="#">Chapter 5</a></li>
            </ul>
        </div>

Answer №2

Give this a shot:

function openMenu() {
    document.getElementById("sidebar").style.display = "inline-block";
    document.getElementById("mysidebar").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
  }

  function closeMenu() {
    document.getElementById("sidebar").style.display = "none";
    document.getElementById("main").style.marginLeft = "0";
  }
<section id="mysidebar">
  <div class="sidebar" id="sidebar" style="display: none;">
    <ul>
      <li><a href="javascript:void(0)" class="closebtn" onclick="closeMenu()">&times;</a></li>
      <li><a href="#">Chapter 1</a></li>
      <li><a href="#">Chapter 2</a></li>
      <li><a href="#">Chapter 3</a></li>
      <li><a href="#">Chapter 4</a></li>
      <li><a href="#">Chapter 5</a></li>
    </ul>
  </div>

  <div id="main">
    <button class="openbtn" onclick="openMenu()">&#9776;</button>
    <h2>Collapsible Sidebar</h2>        
  </div>
</section>

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

Css technique for changing color on mouse hover

I currently have a social media bar on my website with icons for Facebook, Twitter, Google+, and RSS. Here is how it looks: When I hover over the icons, I want the circle around the image to change color to blue. However, every attempt I've made end ...

What causes the error message "ng-model is undefined"?

Within my form, there are three angular-ui-bootstrap typehead elements <form> <div class="form-group"> <label for="fieldname" class="col-md-3 control-label">Name</label> <div class="col-md-6"> <input type="text ...

What is the method for extracting latitude and longitude values individually from JSON data?

Upon receiving the JSON response from the Google Maps API stored in a variable named 'obj', I noticed that alerting obj.name returns "Pancakes on the Rocks". To access the icon, I can use obj.icon. However, I am struggling to retrieve separate va ...

Storing data from a form by utilizing AJAX with PHP

Is there a way to save the form data either in a file or a local database using AJAX, while still sending the data through the form action to an external database? If you want to view the source code for my form, you can find it here: http://jsbin.com/ojU ...

What is the reason for the attribute align="center" functioning in my HTML document?

Recently, I decided to delve into the world of HTML and created a simple basic file: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> My Webpage </title </head> ...

Is there a way to recover a deleted element from an array?

I have a list of user cards, and my task is: Clicking the "undo" button: will restore the last card that was deleted (using an array) Question 1: How can I create an array from the displayed cards list? Question 2: How do I restore the last deleted card? ...

What is the functionality of require(../) in node.js?

When encountering var foo=require(../), what specific modules does node.js search for? It may appear to look in the directory above the current one, but what exactly is it seeking and how does it function? Is there a comparison with include in C or impor ...

How to adjust the timezone settings in PHPMyAdmin on a shared server platform

I'm having trouble changing my timezone to India on my shared server database. I've tried everything but can't seem to get it to work. My website is built using PHP Codeigniter The contact us page on my site saves all inquiry details to my ...

Which should I use - Angular directive or CSS selector, for better performance?

Currently expanding my knowledge on angular and exploring the capabilities of the ngClass directive. I have come across this interesting functionality: <li ng-repeat="language in languages" ng-class="{line:$even}">{{language}}</li> Alternativ ...

Troubleshooting issue with the spread operator and setState in React, Typescript, and Material-ui

I recently developed a custom Snackbar component in React with Material-ui and Typescript. While working on it, I encountered some confusion regarding the usage of spread operators and await functions. (Example available here: https://codesandbox.io/s/gift ...

Having trouble storing data accurately in local storage using React

I implemented a combination of useContext and useEffect to store useContext data in local storage, but I am facing challenges due to conditional rendering. The scenario involves displaying a sign-in button when the user is not logged in and a log-out butto ...

Leveraging jQuery's .when and .then methods allows for

I'm attempting to utilize a shared ajax function that is meant to retrieve the value returned from the PHP script. function fetchAjax(url, data, type) { result = 0; $.when( $.ajax({ type: "POST", url: url, data: data, ...

What is the best way to interact with an element in Python Selenium once it has been located?

I have been working on a script that is supposed to retrieve the attribute of an element and then click on it if the value is true. However, I keep encountering an error in my code. This is the snippet of code I am using: from selenium import webdriver d ...

What is the best method for extracting data from a node?

Issue: Upon sending a get request to node using the fetch API, an error is encountered. Refer to comment in code for details server.js const express = require('express'); const app = express(); const bodyParser = require('body-parser&apo ...

Displaying a distinct image for each Marker when hovering over them on a Map

I have implemented ReactMapGL as my Map component and I am using its HTMLOverlay feature to display a full-screen popup when hovering over markers. However, even though I have set different image data for each marker, all of them show the same image when h ...

Convert a div into a clickable link using JavaScript without using too many classes or any ids

After using shortcodes generated by functions.php in a WordPress parent theme, I have come across the following HTML code: <div class="pricing-table-one left full-width "> <div class="pricing-table-one-border left"> <div class=" ...

The functionality to disable and reenable a button after performing a calculation is not functioning properly in the code

Here's the issue I'm facing with my code: When I click the search button, a for loop prints "hi" 5000 times. Before this loop starts, I want to disable the button. However, after the console.log is done, the button should be enabled again. But fo ...

What is the method for creating a curved CSS Pseudo element background?

Currently, I have implemented a red pseudo element as part of the background for certain sections on my website. Although everything is functioning correctly, I am interested in transitioning the straight line to a curved one instead. Does anyone know ho ...

Blurring of text that occurs after resizing in CSS

When I use scale transform to zoom a div in HTML, the text inside becomes blurred. Is there a solution to make the text clear like the original? @keyframes scaleText { from { transform: scale(0.5, 0.5); } to { transform: scale(2, 2); } } ...

The console is being flooded with API logging messages multiple times

My goal is to develop a search page for Pathfinder. I have crafted the following code in an attempt to retrieve data from the API. During the process of testing the fetch requests, I have noticed that when I console.log the fetched data, it appears multipl ...