The dropdown menu stubbornly refuses to close even after multiple clicks on it in the mobile responsive user interface

Currently, I am tackling the challenge of creating a website navigation bar with a dropdown menu that adjusts to different screen sizes. However, I have encountered an obstacle where the dropdown menu fails to close when I click outside of it. To address this issue, I attempted to incorporate the data-bs-auto-close="outside" attribute into the dropdown toggle element. Regrettably, my efforts proved futile as the dropdown menu still remains open.

Below is a snippet of the pertinent code:

  <!-- Navbar code omitted for brevity -->

  <div class="navbar-collapse collapse show" id="navbarCollapse" style="">
    <div class="container-fluid gx-0">
      <div class="row gx-0 px-lg-2 py-lg-1">
        <div class="col-lg-1 d-flex align-items-center order-lg-1 order-2">
          <!-- Social media icons omitted for brevity -->
        </div>
        <div class="col-lg-11 order-lg-2 order-1 px-2 float-right mb-md-0 mb-1">
          <div class="navbar-nav gap-0 navlinks mt-lg-0 mt-4 nav justify-content-end">
            <!-- Other navigation links omitted for brevity -->
            <a href="#" class="nav-link dropdown-toggle dropdownMenu" id="dropdownMenu" data-bs-auto-close="outside" role="button" data-bs-toggle="dropdown" aria-expanded="false">Menu</a>
            <div class="dropdown-menu mega-menu" aria-labelledby="dropdownMenu">
              <!-- Dropdown menu content omitted for brevity -->
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</nav>
<script src="<?=base_url()?>/<?=$publicimagepath?>frontdesign/js/jquery-3.4.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> The dropdown menu persists in remaining open even after clicking outside of it on mobile devices.

Answer №1

If you want to overcome this issue, simply insert a few lines of JavaScript code into your project

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Interactive Navigation Bar</title>

  <!-- Necessary CSS imports -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8aaa7a7bcbbbcbaa9b888fde6f8e6fa">[email protected]</a>/dist/css/bootstrap.min.css">
  <style>
    .mega-menu {
      /* Your dropdown menu styles go here */
    }
.navbar-nav div:hover>.dropdown-menu{ display: block; background: #291515 !important; padding: 10px 0px; }
  </style>
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="navbar-collapse collapse show" id="navbarCollapse" style="">
      <div class="container-fluid gx-0">
        <div class="row gx-0 px-lg-2 py-lg-1">
          <div class="col-lg-1 d-flex align-items-center order-lg-1 order-2">
            <!-- Omitting social media icons for brevity -->
          </div>
          <div class="col-lg-11 order-lg-2 order-1 px-2 float-right mb-md-0 mb-1">
            <div class="navbar-nav gap-0 navlinks mt-lg-0 mt-4 nav justify-content-end">
              <!-- Skipping other navigation links for brevity -->
              <a href="#" class="nav-link dropdown-toggle dropdownMenu" id="dropdownMenu" data-bs-auto-close="outside" role="button" data-bs-toggle="dropdown" aria-expanded="false">Menu</a>
              <div class="dropdown-menu mega-menu" aria-labelledby="dropdownMenu">
<a>test</t>
                <!-- Omitted dropdown menu content -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </nav>

  <!-- Importing necessary JavaScript files -->
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6d617c6b4e3c2037203c">[email protected]</a>/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36545959424542445746760318061804">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

  <!-- JavaScript solution for dropdown menu -->
  <script>
    document.addEventListener('DOMContentLoaded', function() {
      var dropdownElement = document.getElementById('dropdownMenu');
      var dropdown = new bootstrap.Dropdown(dropdownElement, {
        autoClose: 'outside'
      });
    });
  </script>
</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

What is the best way to import modules in Typescript/Javascript synchronously during runtime?

I have a Typescript class where I am attempting to perform a synchronous import, however, the import is being executed asynchronously. My code snippet looks like this: --------------100 lines of code-------------------- import('../../../x/y/z') ...

Having difficulty with loading images lazily in a jQuery Mobile app with LazyLoadXT feature

Struggling to incorporate lazy loading in my jQM app with Lazy Load XT v1.0.6. Oddly, images only appear when switching browser tabs, not while scrolling down. This happens on Firefox and Chrome. <img src="/img/default-img.jpg" data-src="/img/product/ ...

How to Enhance GraphQL Mutation OutputFields with a Function Generator

I'm encountering issues while trying to incorporate a function generator within a GraphQL mutation. The function generator is utilized to streamline the promise chain inside the mutation. Prior to refactoring the code, everything was functioning corr ...

Display the current count of selected radio buttons in real-time

I am working with radio buttons that are generated dynamically using a 2D array within a while loop. I want to display the number of radio buttons checked when one is clicked. Here are my radio buttons: $n=0; while($row=mysqli_fetch_row($rs)){?> <f ...

Choose the data attributes you want to include and attach them to input elements using plain JavaScript

Creating a User Information form with a select element containing four options, each with data attributes. Under the select are input types to populate the data attributes when selected. Looking for help in achieving this using plain JS and onchange event. ...

Tips for maintaining a sticky header while continuing to utilize Bootstrap table classes such as table-responsive and table-stripped

This is Here's my code on jsfiddle I've attempted to make the header sticky while maintaining the current layout, but every approach I've tried ends up messing with the responsiveness of the table. My next plan involves using a JavaScript ...

Leveraging LevelGraph with MemDOWN

I've been experimenting with using LevelGraph and MemDOWN together, but I've noticed that my put and get queries are significantly slower compared to using the filesystem directly with LevelUP. It seems like there might be some mistake in my setu ...

Adjustable / consistent box height

For hours, I've been attempting to make some divs the same height. Check out my JSfiddle here: https://jsfiddle.net/4cj5LbLs/15/ I experimented with using display: table; in the parent element and either display: table-cell; or display: table-colum ...

search engine optimized for easy input via keyboard

I have successfully incorporated AJAX search functionality into my project, inspired by this example here. The unique feature of this implementation is the ability to navigate through search results using the TAB key. The structure of my search results tab ...

Obtaining the ID from a URL in node.js

As a newcomer to the world of Javascript and node.js, I am venturing into creating a REST API with URLs structured as follows: /user/{userId}/docs The goal is to extract the value of {userId}, which, for instance, in the URL /user/5/docs would be 5. Wh ...

I am having trouble getting the bootstrap link and css files to work on a specific URL. Can you please help me troubleshoot this issue and let me know if there are any additional files needed to

When attempting to apply the bootstrap link and css files to the URL "/list/:customListName", they are not working. However, changing the URL to "/:customListName" somehow makes it work. What is the reason behind this behavior and how can I properly style ...

Ways to align Material-UI component side by side

My MenuList and Grid components are currently stacked vertically, but I need them to be aligned horizontally. I'm wondering why the Grid is nested within a div in the rendered HTML code. How can I make these components behave more like floating HTML e ...

Difficulty encountered while implementing mouseover event listener for an SVG component

I'm having trouble triggering an event for an svg element. Here's the link to my jsfiddle example: https://jsfiddle.net/r1ahq5sa/ Here's the HTML code: <div class="row"> <div class="col-md-8"> <svg class="video-nav-bar ...

Issue with AJAX show/hide causing scrolling to top

I've implemented ajax show hide functionality to display tabs, but whenever I scroll down to the tab and click on it, the page scrolls back to the top. You can check out the example code in this fiddle: http://jsfiddle.net/8dDat/1/ I've attempt ...

Vue.js HTML5 Editor_vueful

I am currently utilizing browserify and attempting to incorporate the vue-html5-editor library from https://github.com/PeakTai/vue-html5-editor. However, when I attempt the following code: Vue.use(require('vue-html5-editor')); An error is thro ...

Complex recurrent operation

Can anyone assist me in solving this complex loop? I need to call 5 API URLs, each a specific number of times before moving on to the next URL in sequence and then starting over again. https://www.example1.com should be called 4 times https://www.example2 ...

In the world of Angular, whitespace is simply treated as non-existent when it comes

Within the controller, I have an array structured as follows: 'Pipe': '|', 'Comma': ',', 'Tab': '\t' }; When configuring ng-options in my code, it is implemented in this manne ...

3 Methods for Implementing a Floating Header, Main Content, and Sidebar with Responsive Design

I am following a mobile-first approach with 3 containers that need to be displayed in 3 different layouts as shown in the image below: https://i.sstatic.net/UjKNH.png The closest CSS implementation I have achieved so far is this: HTML: <header>.. ...

Unlock the full potential of custom authorization providers when integrating them with Supabse

Supabase user here looking to implement authorization with a third-party service that isn't on the supported list. Any suggestions on how to go about this? ...

Youngster listens for guardian's occurrence in Angular 2

While the Angular documentation covers how to listen for child events from parents, my situation is actually the opposite. In my application, I have an 'admin.component' that serves as the layout view for the admin page, including elements such a ...