Trouble with Bootstrap dropdown button functionality when clicked

When attempting to construct a navbar with buttons using bootstrap, I encountered an issue where the dropdowns failed to open upon clicking. The necessary CDN links for bootstrap, ajax popper, and jquery have been correctly included in the code. No external CSS has been added to style the navbar buttons. What could be causing this problem?

Here is a snippet of the code:

 <!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>

    <script src="main.js"></script>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar fixed-top navbar-light bg-light">
        <span class="navbar-toggler-icon"></span>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto ">
                <li class="nav-item">
                    <div class="new-pickup"> <a class="nav-link" id="new-pickup" href="#"><i class="fa fa-plus"id="add-plus" aria-hidden="true"></i>
                     New Pickup&nbsp;&nbsp;&nbsp;&nbsp;</a></div>
                    <li class="nav-item dropdown">
                        <div class="sms-button">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                          &nbsp;SMS&nbsp;&nbsp;
                        </a>
                        </div>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">Delivered Status</a>
                            <a class="dropdown-item" href="#">Log details</a>
                        </div>
                    </li>
                    <li class="nav-item dropdown">
                        <div class="email-button">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            &nbsp;Email&nbsp;&nbsp;
                          </a>
                        </div>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">Deivered Status</a>
                            <a class="dropdown-item" href="#">Log Status</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <div class="call-button">
                            <a class="nav-link" href="#"><i class="fa fa-phone fa-flip-horizontal " aria-hidden="true"></i>
                          Call&nbsp;&nbsp;&nbsp;
                       </a></div>
                    </li>
                </li>
            </ul>
        </div>

    </nav>



    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>

</html>

The main.js file includes the following script:

  $(document).ready(function(){
    $(".dropdown, .btn-group").hover(function(){
        var dropdownMenu = $(this).children(".dropdown-menu");
        if(dropdownMenu.is(":visible")){
            dropdownMenu.parent().toggleClass("open");
    }
    });
});

Answer №1

The reason for this issue could be due to the addition of another div within the code.

To fix this, consider reordering your code so that the element labeled .dropdown-toggle comes after the current item instead of div.sms-button.

Answer №2

Check out the following code examples:

Solution : A

<li class="nav-item dropdown">
    <div class="sms-button" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <a class="nav-link dropdown-toggle" href="#" >&nbsp;SMS&nbsp;&nbsp;</a>
    </div>
    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="#">Delivered Status</a>
        <a class="dropdown-item" href="#">Log details</a>
    </div>
</li>

Solution : B

<li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">&nbsp;Email&nbsp;&nbsp;</a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="#">Deivered Status</a>
        <a class="dropdown-item" href="#">Log Status</a>
    </div>
</li>

Answer №3

Make sure to include

data-toggle="collapse"  data-target=".navbar-collapse"
in your clickable icon element

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
    
        <script src="main.js"></script>

        <nav class="navbar navbar-expand-lg navbar fixed-top navbar-light bg-light">
            <!--code changes here-->
            <span class="navbar-toggler-icon" data-toggle="collapse"  data-target=".navbar-collapse"></span>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto ">
                    <li class="nav-item">
                        <div class="new-pickup"> <a class="nav-link" id="new-pickup" href="#"><i class="fa fa-plus"id="add-plus" aria-hidden="true"></i>
                         New Pickup&nbsp;&nbsp;&nbsp;&nbsp;</a></div>
                        <li class="nav-item dropdown">
                            <div class="sms-button">
                                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                              &nbsp;SMS&nbsp;&nbsp;
                            </a>
                            </div>
                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="#">Delivered Status</a>
                                <a class="dropdown-item" href="#">Log details</a>
                            </div>
                        </li>
                        <li class="nav-item dropdown">
                            <div class="email-button">
                                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                &nbsp;Email&nbsp;&nbsp;
                              </a>
                            </div>
                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="#">Deivered Status</a>
                                <a class="dropdown-item" href="#">Log Status</a>
                            </div>
                        </li>
                        <li class="nav-item">
                            <div class="call-button">
                                <a class="nav-link" href="#"><i class="fa fa-phone fa-flip-horizontal " aria-hidden="true"></i>
                              Call&nbsp;&nbsp;&nbsp;
                           </a></div>
                        </li>
                    </li>
                </ul>
            </div>
    
        </nav>
    
    
    
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

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

The HTML date input is displaying the date incorrectly, appearing one month off when set via the value attribute

i have tried this in two different browsers 1.Chromium Version 76.0.3809.87 2.Mozilla firefox 68.0.1 every time i enter a date in the input field in the format y-m-d, the month seems to be reduced by one without any explanation this is my code <i ...

Explore Earth in 360 degrees with mouse interactions using threejs

I am facing an issue. I want to rotate the earth in 360° using my mouse, but nothing is happening. However, I would like the world to remain fixed and not move when I rotate it with the mouse. Looking forward to a solution. <!DOCTYPE html> ...

Unusual perspective of JSON with ng-jsoneditor in AngularJS

Currently, I have integrated ng-jsoneditor into my AngularJS application to display and format JSON data. I found guidance on how to implement this from both here and here. Here is the HTML code snippet: <div ng-jsoneditor="onLoad" ng-model="vm. ...

Is there a way to arrange my bootstrap navigation tabs vertically on my mobile device?

I have a group of five bootstrap navigation tabs that are evenly spaced and visually appealing on desktop screens. However, when viewed on a mobile device, the text within the tabs becomes cramped together. How can I make the tabs stack vertically on mobil ...

:hover does not activate the animation forwards

Why won't the animation: forwards work on :hover? The animation itself is functioning properly, but the command "forwards" isn't. The Issue: Here's the HTML code snippet: <img src="images/notebook_desk.svg" class="notebook-cover"> ...

What are the steps to modify the text color in WKWebView?

I've customized my ViewController to include a WKWebView, where I load my content from both .html and .css files. The result resembles a controller for reading books within the Apple Books app. do { guard let filePath = Bundle.main.path(fo ...

Flow bar for micro-tasks

In my current project, I am faced with the task of organizing a series of 4 mini tasks and displaying to the end user which specific mini task they are currently on. To accomplish this, I have been utilizing image tags for each task. <img>1</img ...

unable to choose an option if more than one radio button is being used

I am having trouble with two radio buttons that are supposed to select the gender of an applicant and the gender of the applicant's child. Both buttons are not functioning properly. Can someone assist me with this issue? Here is the code I am using: ...

Navigating through the conditional "where" clause in IndexDB for various browsers

I have successfully implemented indexdb because it provides support for all browsers. I have managed to add and retrieve data from indexdb, but now I want to implement a where clause condition. For example, I have fields like Product Name, Pathogen, Diseas ...

Items are overflowing the flex container, exceeding its boundaries

I've experimented with the width property, flex, flex-basis, and flex-shrink, but so far nothing seems to be working. I can't seem to pinpoint where the mistake lies in the code. I wanted to include the code snippet here, but it's throwing ...

How to Deactivate Horizontal Scrolling Using CSS in WordPress

Hey there, I'm currently working on a WordPress blog and facing an issue. Here's the link to my problem: When I resize the browser (X-Axis) to a minimum, like when using a mobile device, I noticed that I can scroll to the right in the Content se ...

Trouble getting JavaScript variables to properly insert into strings when sending them to an HTML document

Struggling with variable interpolation in JavaScript strings. I need to replace the words_to_be_removed variable with the defaultMessageTest string, but none of my attempted methods are working. Here are the three methods I tried: String text ${expressi ...

Issue with Bootstrap 4 Dropdown Menu not directing to specified href destination

Project URL: You can download the file by clicking on "Download bootstrap menu file for testing" I have implemented a Bootstrap 4 hover menu that works fine on desktop, but on mobile devices, it converts to a clickable menu. Everything is functional so ...

Tips for retrieving database records based on the chosen value in an HTML date picker

I need help with using an HTML date picker on my website. <form action="" method="POST"> <input type="date" name="date"> <input type="submit" style="position: relative; bottom:100px; ...

What is the method for displaying text and icons as black in a sticky header design?

Having trouble making the menu text and icons in the sticky header black on my website. I've been searching for the correct class in the CSS styles, but haven't been able to find it. I tried using this CSS: .header-wrapper .stuck { color: #000 ...

Guide to accessing a string from a file with PHP

So I've got this text file called mytxt.txt The content inside is formatted like this: name (username) name (username) I'm attempting to extract these lines and insert them into HTML <li> elements, but I'm struggling with how to do ...

Step-by-step guide on using the input tag to embed videos

I'm trying to embed a YouTube video using an iframe with an input tag, but for some reason, it's not working. Can you help me find the mistake? Here's the URL I entered: https://www.youtube.com/embed/G20AHZc_sfM This is the code in the body ...

Scroll Behavior Issue in Bootstrap 4.4.1 Accordion Component on Google Chrome - Possible Bug?

I've been using Bootstrap accordions for quite some time, but a new issue has recently cropped up. If you play around with the code below (preferably in full screen mode), scroll down until the accordion is visible, then open the middle card by toggl ...

Launching a URL in a pop-up window with customized title and address bar

I am seeking to implement a feature similar to the following: <a href="post/23">post 23</a> When a user clicks on this element, I want a popup div to fade in and load the HTML from page post/23 into it. Additionally, I would like the title an ...

Utilize Google Maps API to showcase draggable marker Latitude and Longitude in distinct TextFields

I am currently utilizing the Google Maps example for Draggable markers. My objective is to showcase the latitude and longitude values within separate TextFields, where the values dynamically change as I drag the marker. Once the user stops dragging the mar ...