"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

Is it possible to alter preact-material-components to switch to mdc-theme--dark mode, thereby changing the CSS style of all descendant components?

I recently created a preact-cli app and added the following code in Header.js: document.body.classList.add('mdc-theme--dark'); However, when a user tries to switch from the light theme to the dark theme, only the background of the app changes. ...

Is a single f.select impacting another f.select in the same form? (undesired)

I am facing an issue where adding a new HTML element like: <%= f.date_select :date, { id: "date-select"} %> is impacting my existing collection select: <%= f.collection_select :id, Customer.where(business_id: current_c ...

Issue with iframe's size not displaying correctly

<body> <script> document.write("<iframe id='theframe' src='http://www.website.com?testvr=" + testvr + " height='900' width='500'></iframe>"); </script> </body> The iframe is added, but ...

Sending data to a JSON file with jQuery and AJAX: Step-by-step guide

While there are numerous tutorials available on how to retrieve data from json files using jQuery and ajax, I have been unable to find any that demonstrate how to POST data to a json file. If anyone has a script or example they could share with me on how t ...

Using Webflow code in ReactJS/NextJS: Best practices and implementation tips

After exporting my Webflow code to my NextJS app, I noticed that many of the HTML tags are being flagged as errors because they appear to be missing closing tags. Can anyone shed some light on why this is happening and suggest potential solutions? It&apos ...

Tips for correctly positioning CSS elements:

I am currently working on a slider using noUi Slider and aiming for an elegant solution. To accommodate the large size of the handle, I expanded the base UI with extra values which are not allowed, causing the handle to jump back to the permitted values. T ...

Exploring "Additional resources" using Selenium and Python

I am looking to extract text from the "Further reading" section of a Wikipedia article. My current code can open Google, input a request (such as 'Bill Gates'), and then find the URL of the corresponding Wikipedia page. Now I need help parsing th ...

What steps can I take to ensure that my logos remain visible even when I close the menu and resize the window?

On my website, I have a menu of logos that are clickable. These logos always display except on smaller screens, where they need to be toggled to show using a hamburger menu. The menu toggles fine when it is on a smaller screen, but there is an issue when y ...

The functionality of a generated button has been compromised

My goal is to create a webshop that doesn't rely on MySQL or any database, but instead reads items from JSON and stores them in LocalStorage. However, I've encountered an issue where the functionality of buttons gets lost after using AJAX to gene ...

Retrieve the following item belonging to a specific class immediately after the current selection

On my website, I have multiple comment sections scattered throughout. For example, you can check out one of them here: This particular site is dedicated to questions and answers. As you navigate through the page, you will notice a Grey "Write a reply..." ...

``A problem with the background image of the left panel in Framework 7

After setting a background image for the side panel and blurring it using CSS, I encountered an issue where the text and icons within the side panel also became blurred. Despite attempting to isolate the background image in a separate class, the problem ...

Show information according to the selection made in the dropdown menu

Greetings! I am currently developing a web app using MVC .NET (C#) that includes a registration page for two types of users, type 1 and type 2. In the form, there is a dropdown list where users can select their type. When selecting type 2, I would like t ...

The battle between Hover, Focus, and Blur modes intensifies

My goal is to implement 4 different effects on an element: When hovering over the element. When moving away from the element. When focusing on the element. When blurred. However, I'm encountering a conflict where when I focus on the element, the ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

Jquery selector failing to target correct <form> element within Shopify theme

I'm feeling a bit lost here! Trying to zero in on a form within my page and utilize jQuery's serializeArray() function to extract all the values from said form. <div class="page-width"> <header class="section-header ...

Tips for increasing the size of the SVG icon within Material UI iconButtons

Have you experimented with creating webpages using react.js along with the Material UI library? How can I adjust the size of an SVG icon? Recently, I developed a "create new" component, which consists of a material paper element with an add button on top. ...

Exploring the Fusion of Strings and Arrays of Javascript Objects using jQuery and JSON

Trying to achieve a simple task, but not very proficient in jQuery so struggling to figure it out. Want to send JSON data to an ASP.NET Controller. Data includes strings and a list of objects. The code snippet would appear like this: View: $(document). ...

Optional parameters in Sammy.js

Utilizing ajax for paging has led me to choose Sammy.js, which works well. However, incorporating checkboxes to filter results poses a challenge. While defining a route for Sammy to intercept is feasible, the issue arises when I wish to avoid displaying ce ...

What is the user-agent string for the Safari home screen?

Is there a unique user-agent string specifically for Safari on IOS that identifies it as being in "Home screen" or "app mode"? I've observed a bug on IOS8 where the browser window appears incorrectly, with the time and battery information overlapping ...

Styling of checkboxes in jQuery Mobile is missing after an AJAX request

I am currently working on implementing an ajax call to retrieve a list of items from a json array and display them as checkboxes. While the items are loading correctly, they lack the jquery mobile styling. $(document).ready(function(){ ...