Having trouble with the JSFiddle dropdown button that is unresponsive to

I am currently in the process of developing a test drop-down menu. The open menu button functions correctly, however, the close button is unresponsive to clicking or hovering actions. Upon clicking the open menu button, it should make the other button visible and clickable as well.

The issue with the close button seems to lie in its display property being reset to block upon click events.

function toggleMenu() {
  var hamburger = document.getElementById("burg");
  var crossIcon = document.getElementById("cross");

  var freezeBody = document.getElementById("body_freeze");

  var navList = document.getElementById("nav_list");

  var navItems = document.getElementsByClassName("nav_item");

  if (hamburger.style.display === 'none') {
    hamburger.style.display = 'block';
    crossIcon.style.display = 'none';
    navList.style.display = 'none';

    freezeBody.style.position = 'relative';
  } else {
    hamburger.style.display = 'none';
    crossIcon.style.display = 'block';
    navList.style.display = 'block';

    freezeBody.style.position = 'fixed';
  }
}
.fixed_nav {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  height: 50px;
  background-color: white;
  border-bottom: 1px solid black;
  box-shadow: 0 4px 8px 0 rgb(0, 0, 0, .2);
}

#burg {
  z-index: 3;
  float: right;
  display: block;
  margin: 20px;
  width: 100px;
  height: auto;
}

#burg:hover {
  background-color: gray;
}

#cross {
  z-index: 3;
  margin: 20px;
  display: none;
  float: right;
  width: 100px;
  height: auto;
}

#cross:hover {
  background-color: gray;
}

#nav_list {
  height: 100vh;
  width: 100%;
  background-color: white;
  display: none;
  z-index: 1;
}

#dummy_text {
  margin-top: 50px; 
}

.nav_item {
  margin-top: 50px;
  color: red;
  width: 100%;
  background-color: white;
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  z-index: 2;
}

#body_freeze {}
<html>

<head>
  <title>ExampleError</title>
</head>

<body>
  <nav class="fixed_nav">
    <button id="burg" onclick="toggleMenu();">
  &#9776;
  </button>

    <button id="cross" onclick="toggleMenu();">
  &#735;
  </button>
    <ul id="nav_list">
      <div class="nav_item">
        <a href="#">
          <li>Inventory</li>
        </a>
      </div>

      <div class="nav_item">
        <a href="#">
          <li>Finance</li>
        </a>
      </div>  
    </ul>
  </nav>

  <div id="body_freeze">
    <p id="dummy_text">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...<!--Text continues-->
    </p>
  </div>
</body>

</html>

Answer №1

The issue lies in your code where you define the "body_freeze" element as fixed but do not specify its exact position, causing it to overlay the button and prevent clicking. By adding position, top, and z-index properties to the body_freeze ID in the style definition, I was able to resolve this problem.

function menu_change() {
  var burg = document.getElementById("burg");
  var cross = document.getElementById("cross");

  var body_freeze = document.getElementById("body_freeze");

  var nav_list = document.getElementById("nav_list");

  var nav_items = document.getElementsByClassName("nav_item");


  if (burg.style.display === 'none') {

  burg.style.display = 'block';
  cross.style.display = 'none';
  nav_list.style.display = 'none';
  
  body_freeze.style.position = 'relative';

  } else {
  
  burg.style.display = 'none';
  cross.style.display = 'block';
  nav_list.style.display = 'block';
  
  body_freeze.style.position = 'fixed';
  
  }


}
.fixed_nav {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  height: 50px;
  background-color: white;
  border-bottom: 1px solid black;
  box-shadow: 0 4px 8px 0 rgb(0, 0, 0, .2);
}

#burg {
  z-index:3;
  float: right;
  display: block;
  margin: 20px;
  
  width: 100px;
  height: auto;
}

#burg:hover
{
  background-color: gray;
}

#cross {
  z-index:3;
  margin:20px;
  display: none;
  float: right;
  width:100px;
  height:auto;
}

#cross:hover
{
  background-color: gray;
}

#nav_list {
  height: 100vh;
  width: 100%;
  background-color: white;
  display: none;
  z-index: 1;
}

#dummy_text {
  margin-top: 50px;
}

.nav_item {
  margin-top:50px;
  color: red;
  width:100%;
  background-color:white;
  border-top:1px solid black;
  border-bottom: 1px solid black;
  z-index:2;
}

#body_freeze {
position: relative;
top: 100px;
z-index: 1;
}
<html>

  <head>

    <title>ExampleError</title>
  </head>


  <body>
    <nav class="fixed_nav">

      <button id="burg" onclick="menu_change();">
  &#9776;
  </button>

      <button id="cross" onclick="menu_change();">
  &#735;
  </button>

      <ul id="nav_list">

        <div class="nav_item">
          <a href="#">
            <li>Inventory</li>
          </a>
        </div>

        <div class="nav_item">
          <a href="#">
            <li>Finance</li>
          </a>
        </div>
      </ul>
    </nav>

    <div id="body_freeze">



      <p id="dummy_text">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lobortis elementum nibh tellus molestie nunc non blandit massa. Sit amet purus gravida quis blandit. Ac feugiat sed lectus vestibulum
        mattis ullamcorper velit. Odio ut enim blandit volutpat maecenas volutpat blandit aliquam etiam. Egestas sed tempus urna et pharetra pharetra massa massa. Enim lobortis scelerisque fermentum dui faucibus in ornare quam. Rutrum tellus pellentesque
        eu tincidunt tortor aliquam nulla facilisi. In pellentesque massa placerat duis. Erat velit scelerisque in dictum non. Massa placerat duis ultricies lacus sed turpis tincidunt. Urna molestie at elementum eu facilisis sed odio. Ornare arcu odio
        ut sem nulla. Consequat interdum varius sit amet mattis vulputate. Justo donec enim diam vulputate ut. Etiam dignissim diam quis enim lobortis scelerisque fermentum dui. Risus at ultrices mi tempus. Vestibulum morbi blandit cursus risus at ultrices
        mi tempus. Ante metus dictum at tempor commodo. Turpis nunc eget lorem dolor. Leo vel orci porta non pulvinar neque. Volutpat sed cras ornare arcu dui vivamus arcu felis bibendum. Proin sed libero enim sed faucibus. Lacus vel facilisis volutpat
        est velit. Scelerisque eleifend donec pretium vulputate sapien. Curabitur gravida arcu ac tortor. Massa eget egestas purus viverra accumsan in nisl nisi scelerisque. Viverra aliquet eget sit amet tellus cras. Massa tempor nec feugiat nisl pretium
        fusce id velit ut. Ultrices sagittis orci a scelerisque. Bibendum arcu vitae elementum curabitur vitae nunc sed. Enim ut sem viverra aliquet. Placerat vestibulum lectus mauris ultrices eros in cursus turpis. Sed euismod nisi porta lorem mollis
        aliquam. Ridiculus mus mauris vitae ultricies. Ullamcorper velit sed ullamcorper morbi. Adipiscing elit pellentesque habitant morbi tristique. Nibh venenatis cras sed felis eget velit. Leo integer malesuada nunc vel risus commodo viverra maecenas
        accumsan. Porttitor eget dolor morbi non arcu risus quis varius. Tortor aliquam nulla facilisi cras fermentum odio eu. In iaculis nunc sed augue lacus viverra vitae congue. Ac felis donec et odio pellentesque diam volutpat. Ut lectus arcu bibendum
        at varius vel pharetra vel. Feugiat in ante metus dictum at. Diam sit amet nisl suscipit. Nam aliquam sem et tortor. Arcu ac tortor dignissim convallis aenean et. Sed risus ultricies tristique nulla aliquet enim tortor at. Rhoncus dolor purus
        non enim praesent elementum facilisis. Integer feugiat scelerisque varius morbi enim nunc faucibus a. At varius vel pharetra vel turpis nunc eget lorem. Quam adipiscing vitae proin sagittis. Gravida cum sociis natoque penatibus. Eu feugiat pretium
        nibh ipsum consequat nisl vel. Sagittis aliquam malesuada bibendum arcu vitae elementum curabitur. Fringilla ut morbi tincidunt augue interdum velit euismod in. Enim tortor at auctor urna nunc. Dolor sed viverra ipsum nunc aliquet bibendum enim
        facilisis gravida. Arcu cursus euismod quis viverra nibh cras pulvinar mattis. A diam maecenas sed enim. Tortor id aliquet lectus proin nibh nisl condimentum. Ut enim blandit volutpat maecenas volutpat blandit aliquam. Venenatis tellus in metus
        vulputate. Mollis nunc sed id semper risus. Lacus luctus accumsan tortor posuere ac ut consequat semper. Euismod nisi porta lorem mollis aliquam ut porttitor leo a.
      </p>


    


    </div>


  </body>

</html>

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

Using a Regex Pattern to Validate Password Strength

var pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()~])[a-zA-Z0-9!@#$%^&*()~]+$/; var password = document.getelementbyId("txtPassword").value; if(!pattern.test(password)) { alert("Invalid Password. Please make sure it contains at ...

Enhancing User Experience: Creating a Vue Button Component for Adding Items to Cart with the Power of Axios and Laravel Backend Integration

I have recently developed a Vue3 shopping cart with an "Add One" button feature. When the user clicks this button, it updates an input field of type "number" and sends a request to update the database using Axios along with Laravel's createOrUpdate me ...

Exploring the capabilities of the Next.js router and the useRouter

import { routeHandler } from "next/client"; import { useRouteNavigator } from "next/router"; const CustomComponent = () => { const routerFromHook = useRouteNavigator(); } export default CustomComponent; Can you explain the disti ...

AngularJS, building a hash of resources

Is there a way, in an AngularJS controller, to take a URL and redirect that request to the best place for fetching JSON data? The VideoSearchCtrl is connected to the search form. Everything seems fine with the generated URL for the template, so I aim to us ...

Tips for presenting JSON date in JavaScript using Google Chart

I am in urgent need of assistance with this issue. I am trying to display the date from PHP JSON data retrieved from my database in a Google Chart using JavaScript. Below is the PHP code snippet: $data_points = array(); while($row = mysqli_fetch_array($r ...

Mastering the utilization of the Data object in VueJS with Decorators can be tricky. One common error message you might encounter is, "Class method 'data' expected 'this' to be used."

Error > The class method 'data' should use 'this' but it is not. I encountered this issue and believed I fixed it as shown below: TypeScript Unexpected token, A constructor, method, accessor or property was expected <script lang ...

Leverage scope Variable in Angular Service URL

Currently, I am aiming to retrieve data from an API by sending specific search parameters through an AngularJS service. Within my ng-model, I have a variable named "search" that I want to utilize as a parameter in the API URL. My initial (unsuccessful) at ...

Is there a way to display only the specific child div within the parent div using JavaScript without affecting the others?

   **** Sorry for the lengthy title **** Today I encountered a problem that I would like to discuss with all of you. When I click on a "COMMENT" button, instead of triggering JavaScript code to display a CHILD.div inside the corresponding ...

AgGrid's magical dropdown feature

Struggling to integrate a bootstrap-4 dropdown menu with AgGrid, I'm facing an issue where the data table overlaps the dropdown when the html is present. Attempts to increase the z-index have not yielded results. Below is the html code snippet: < ...

Deciphering HTML within a Vue attribute: Tips and tricks

I'm having trouble converting some code to Vue. There seems to be a rendering difference in the alt attribute of an image, and I can't figure out how to interpret the html within the attributes. I've searched for solutions but haven't f ...

JavaScript functionality is not operational when accessed from a network drive

I'm having an issue running the following file. It works perfectly when run from a local drive but fails to execute when placed on a network drive. Any insights on why this may be happening? The code snippet below is what I am attempting to run, util ...

default selection in AngularJS select box determined by database ID

Here is the scenario: ... <select ng-model="customer" ng-options="c.name for c in customers"> <option value="">-- choose customer --</option> </select> .... In my controller: $scope.customers = [ {"id":4,"name":"aaaa", ...

Box with negative z-index cannot be clicked

I am currently facing an issue with a textbox in the center of my screen - when I click on it, no actions are registered. This leads me to believe that there may be a CSS Z-Index problem at play here. How can I troubleshoot and correct this issue effective ...

What is causing the alt or title tags to not function properly in emails?

I am currently facing an issue with e-mail templates that include images. The problem arises from the fact that these images need to be downloaded before they can be displayed in the email. To work around this, I always use ALT and TITLE tags to provide de ...

Styling Page Titles with CSS Content

I'm having trouble including the page title in the footer of all printed pages. I've tried using the following code snippet: @page { @bottom-right { content: attr(title); } } However, this method doesn't seem to be working for me. ...

Position elements to the right side of a flex container

I'm utilizing Bootstrap 4 along with this CSS class to vertically align my elements: .vertical-align { display: flex; flex-direction: row; } .vertical-align > [class^="col-"], .vertical-align > [class*=" col-"] { display: flex; align-i ...

Use jQuery in MVC 5 to remove each cloned div individually starting from the last one, excluding the default div

My default setting is as shown below. https://i.stack.imgur.com/m8Hpr.png The add button functions correctly, but I am having trouble removing cloned divs. I can only remove the last one, instead of each cloned div individually starting from the last, exc ...

The React application functions smoothly when executed using react-scripts start, but encounters an "Unexpected SyntaxError: Unexpected Token: <" error upon building

My goal is to deploy my portfolio site using an express server and react-scripts build. Everything works perfectly fine when I run react-scripts start. However, when I try to serve up the build index.js, I encounter the following errors: 2.8b4a0c83.chunk.j ...

What is the best way to store images in a directory using JavaScript and ASP.NET?

How can I upload and save an image in a folder using ASP.NET, then call and display it? Is it possible to achieve this using AJAX, jQuery, or JavaScript with Web Method? <asp:FileUpload CssClass="image" ID="fileUpload" runat="server" /> I currently ...

The fs.fsync(fd, callback) function in the node.js API allows you

Can you explain the purpose of fs.fsync(fd, callback) in the Node.js API? fs.fsync(fd, callback) This function is used for asynchronous fsync(2). The completion callback only returns an exception if there is one. fs.fsyncSync(fd) This function is for ...