"Trouble with Bootstrap 4 menu: only one dropdown opens at a time

I am facing an issue where the text remains a link and the dropdown toggle works, but it is causing Dropdown-2 to open when I click on Dropdown-1. Only Dropdown-1 seems to be working properly.

I can't seem to figure out why only Dropdown-1 opens no matter which dropdown I click on.

My code is using Bootstrap 4.4.1

You can view the Pen here https://codepen.io/cr8tivly/pen/MWavBJr

This is the markup:

<nav class="navbar navbar-expand-lg navbar-light"> 
  <ul class="row row-cols-4 navbar-nav main relative list-unstyled col-6"> 
    <li class="col nav-item dropdown" data-toggle="dropdown" aria-expanded="false"> 
      <a class="" href="http://google.com">Dropdown-1</a> 
      <ul class="dropdown-menu"> 
        <li>item-1-one</li> 
        <li>item-2-one</li> 
        <li>item-3-one</li> 
        <li>item-4-one</li> 
      </ul> 
    </li> 
    <li class="col nav-item dropdown" data-toggle="dropdown" aria-expanded="false"> 
      <a class="" href="http://google.com">Dropdown-2</a> 
      <ul class="dropdown-menu"> 
        <li>item-1-two</li> 
        <li>item-2-two</li> 
        <li>item-3-two</li> 
        <li>item-4-two</li> 
      </ul> 
    </li> 
    <li class="col"><a href="#">Three</a></li> 
    <li class="col"><a href="#">Four</a></li> 
  </ul> 
</nav> 

Answer №1

The issue arises when data-toggle="dropdown" is placed on the a element instead of the li. Please refer to the official documentation for more details.

Instead of this code snippet:

<li class="col nav-item dropdown" data-toggle="dropdown" aria-expanded="false"> 
  <a>Dropdown-1</a>

It should be corrected to:

<li class="col nav-item dropdown" aria-expanded="false"> 
  <a data-toggle="dropdown">Dropdown-1</a>

Find the updated version in this CodePen link.

Answer №2

To have the plus sign trigger the dropdown menu and the text act as a link, you can implement the following code:

<li class="col nav-item">
  <div class="d-flex">
    <a class="btn btn-link" href="http://google.com">Dropdown-2</a> 
    <button class="btn btn-light dropdown" id="d2" data-toggle="dropdown" aria-expanded="false">+</button>
    <ul class="dropdown-menu" aria-labelledby="d2"> 
     <li>item-1-two</li> 
     <li>item-2-two</li>
     <li>item-3-two</li> 
     <li>item-4-two</li> 
    </ul>
  </div> 
</li>

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

Different methods to retrieve content within a div that is located on a separate, included page

It's a bit tricky to come up with a title for this topic, but essentially, I'm trying to figure out how to get content from an included page into a specific div using HTML and PHP. Let me break it down: In the header.php file, we have the follo ...

Tips for eliminating the page URL when printing a page using window.print() in JavaScript or Angular 4

Can someone help me with a function that uses window.print() to print the current page, but I need to remove the page URL when printing? I'm looking to exclude the URL from being printed and here is the code snippet where I want to make this adjustme ...

Position the Bootstrap 5 navbar at the top of the page and set it

Is there a way to use the fixed-top class on a navbar without making it full width and overlapping the container? This is the code: <div class="container" style="border: 1px solid;"> <nav class="navbar fixed-top n ...

Place a checkmark icon on the chosen card

In my current project, I am developing a patient management system that includes an interface displaying a list of patients in horizontal card shapes. However, when I click on a patient card, I want a check icon (in the form of an SVG) to overlay at the ri ...

Creating HTML form input fields for reading and writing an XML file

Currently working on constructing an HTML form containing input fields to read and modify data from an XML file. The initial task involves loading values into the input fields upon page load, but unfortunately, it is not functioning as expected. < ...

React app version displaying incorrect links to CSS and JS files

I have been immersed in a React project called Simple-portfolio, you can find the GitHub repository here: https://github.com/Devang47/simple-portfolio and the live site at this URL: While everything works smoothly on the development server, I encountered ...

The Hamburger Menu in the Bootstrap Navbar is failing to collapse

My website uses Bootstrap v5.3 and I've added a navbar, but the hamburger icon is not collapsing on mobile devices when clicked. Can someone please review my code to see if there are any issues? I can't figure out what's wrong. <!DOCTY ...

What is the best way to remove the background transparency of an image?

Images are being shown inside an HTML table using PHP code : This is the code for the table : for ($i = 0; $i < $data['list']['cnt']; $i++) { $tabRows[$i][1]['width'] = "45%"; $tabRows[$i][1][&apos ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

Exploring the Mathematical Capabilities of jQuery

I have a question regarding a jQuery code that retrieves the price of Bitcoin from a JSON API and displays its current value in euros. I am interested in converting this to another currency, such as Danish Crown or any other. Essentially, my query is whe ...

Update Relative Links using JQuery or JavaScript

Currently, I am employed by a company that specializes in building websites using a specific platform. We have encountered an issue related to relative URLs within this platform. The platform exclusively utilizes relative URLs and I am looking for a way t ...

Generating dynamic content

I require assistance with a programming issue. I am working with two separate components: Stage and Executor. Within the Stage component, I am attempting to create new elements based on input parameters, while in the Executor component, I set these paramet ...

Activate divs with Bootstrap5 modal toggle functionality

What adjustments must be made to the Bootstrap 5 example below in order to achieve the following two objectives: The "afterAcceptingTerms" division should remain hidden until the user clicks on the Accept Terms modal button, ensuring that only the "before ...

A straightforward method for tracking clicks using jquery, mysql, and ajax

Here's a question for efficiency: What is the most streamlined method for tracking button clicks, saving that data to a WordPress MySQL database, and then updating the total count in a div using Ajax without having to refresh the page? I am explorin ...

Modify the fixed div's class when the user scrolls past a certain section

My page includes a fixed menu with various sections, each assigned a specific class name using data attributes (e.g. data-menu="black"). I want the fixed menu to change its class based on the section that is currently underneath it as the user scrolls. Y ...

Using the setTimeout function is essential for successfully extracting values in a Jquery each loop. Without

I created a loop to adjust the padding on an image tag placed atop another image. Strangely, it was only capturing the first one or two values until I added a setTimeout function, which seemed to fix the issue. Below is the jQuery code: $(document).ready ...

managing the HTML class names and IDs for various functions such as styling, jQuery interactions, and Selenium automation

While there is an abundance of articles on writing clean HTML/CSS, one aspect that seems to be lacking advice is how to organize class names and IDs for different purposes such as design, jQuery, and Selenium testing. The challenge lies in deciphering the ...

The jQuery Mobile click event is limited to firing only once, as the DOM undergoes changes as a result of the initial

I am attempting to attach a click event using jQuery Mobile in a multi-page setting (this page is not the main page): HTML <div data-role="page" id="thirdPage"> <div class="wrapper"> <div class="segment"></div> < ...

Various image dimensions cause divs to shift unevenly

Here is the current setup: <div class="row"> <div class="col-md-4 content-column"> <img class="btn-center btn-desktop" src="images/buttons/btn-desktop.svg" alt="desktop button"> <h2 class="btn-deskt ...

The top margin is experiencing issues and is not functioning correctly

I'm struggling to identify the problem with this script: http://jsfiddle.net/AKB3d/ #second { margin-top:50px; ... } My goal is to have the yellow box positioned 50px below the top border of the right box. However, every time I add margin-top to ...