The drop down menu is refusing to close with a second click or touch

I'm currently using a bootstrap template that I found online, but I've encountered an issue where the drop-down menu for "Shop" doesn't close on the second click when viewing it on a smaller screen size. Can anyone provide guidance on how to fix this? Here is the code snippet:

$('nav .dropdown').hover( function(e) {
        e.stopPropagation(); 
        $('.dropdown-menu', this).stop( true, true ).fadeIn("fast");
        $(this).toggleClass('open');
        $('b', this).toggleClass("caret caret-up");                
    },
    function(e) {
        e.stopPropagation(); 
        $('.dropdown-menu', this).stop( true, true ).fadeOut("fast");
        $(this).toggleClass('open');
        $('b', this).toggleClass("caret caret-up");                
    });


    $('#dropdown04').on('show.bs.dropdown', function (e) {
        console.log('show');
    });

    $('.dropdown a').on('click', function (event) {
        e.stopPropagation(); 
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-dark ftco_navbar bg-dark ftco-navbar-light" id="ftco-navbar">
        <div class="container">
          <a class="navbar-brand" href="index.html">Vegefoods</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#ftco-nav" aria-controls="ftco-nav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="oi oi-menu"></span> Menu
          </button>

          <div class="collapse navbar-collapse" id="ftco-nav">
            <ul class="navbar-nav ml-auto">
              <li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
              <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" id="dropdown04" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Shop</a>
              <div class="dropdown-menu" aria-labelledby="dropdown04">
                <a class="dropdown-item" href="shop.html">Shop</a>
                <a class="dropdown-item" href="cart.html">Cart</a>
                <a class="dropdown-item" href="checkout.html">Checkout</a>
              </div>
            </li>
              <li class="nav-item"><a href="about.html" class="nav-link">About</a></li>
              <li class="nav-item"><a href="contact.html" class="nav-link">Contact</a></li>
              <li class="nav-item cta cta-colored"><a href="cart.html" class="nav-link"><span class="icon-shopping_cart"></span>[0]</a></li>

            </ul>
          </div>
        </div>
      </nav>
    <!-- END nav -->

Any assistance would be greatly appreciated.

Answer №1

To effectively open/close the dropdown, start by removing the attribute data-toggle="dropdown". Handle the click event with the following code:

$('a.dropdown').on('click', function () {
 $(this).parent().toggleClass('open'); 
});

Next, implement a way to close the dropdown when clicked outside of it using this code:

$('a.dropdown').on('click', function () {
 $(this).parent().toggleClass('open'); 
});

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

Ways to decrease the height of the navigation bar in Bootstrap version 4.1

Struggling to figure out how to decrease the height of the navbar in bootstrap v4.1. I want it to be specifically 24px tall; Thanks, Richard. ...

Angular.js: Oops! Looks like the 'MyApp' module is missing

Can anyone help me with using this plunk locally on my machine? I've been trying to run it with the local Python server or http-server, but I keep encountering this Error message: Uncaught Error: [$injector:modulerr] Failed to instantiate module myAp ...

CSS designs overlapping

I am facing an issue with a modal window that I dynamically load onto the client's website. The modal includes certain CSS styles, but sometimes these styles cause my form to break. For instance, I have an input with the class .my_btn { backgroun ...

Having trouble getting the CSS transition effect to work properly

With the click of a button, I alternate the opacity of an element between 0 and 1. A transition effect is supposed to take place for the opacity property. However, the transition effect does not seem to work as expected. Check out this link to see the p ...

How to design 'Sliding gallery panels' using jQuery and CSS3?

Usually, I know how to start a project with tutorials or some experience, but this time I'm stuck on what to even call it. Simply defining the concept could help me search for the right resources. Here's a quick mockup I put together: I want to ...

Generate a dynamic jqplot chart using data retrieved from a variable and JSON data

I'm struggling with loading data into a jqplot chart via variables as it only displays the first value. I am puzzled as to why this is happening. JavaScript: $(document).ready(function () { var sin = [[20, 10, 0, 10, 15, 25, 35, 50, 48, ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

Adjust the divs right element by adding or removing 1 pixel for every size change in the browser

I have been exploring different approaches to achieve this task and it seems like using javascript might be the most effective way. I am currently dealing with a stubborn social icon container placement issue. More details on my previous question can be fo ...

Accordion menu with smooth CSS3 transitions

I created a unique accordion menu to replace the select form control, and I am interested in using CSS3 transitions to give it a smooth expand and contract effect. Feel free to check out my work on jsfiddle: http://jsfiddle.net/hKsCD/4/ In order to achi ...

Is it possible to dynamically update the contents of a modal body and modal footer using

I'm dealing with a modal that dynamically populates two sections: modal-body and modal-footer. However, the issue is that only the content of modal-body changes dynamically while modal-footer remains static. Here's an example of the HTML code (w ...

ngDraggable does not function properly when the dropzone is larger and includes a scrollbar on the body

Here is a demo showing the issue: The current version 0.1.11 does not support drag and drop functionality. This is how I have implemented the code: <uib-accordion-group is-open="category.open" name="category-{ ...

The event tooltip in fullcalendar does not show up as expected

Within my Laravel 5.8 / Bootstrap v4.1.2 / jQuery jQuery v3.3.1 fullcalendar v4.3.1 application, I am attempting to incorporate tooltips for events in the fullcalendar plugin. To achieve this, I referred to a specific example provided here: Sample Example ...

What is the most effective way to combine cells within an HTML table?

I am struggling with merging cells in the first row of a table. I would really appreciate some guidance on how to resolve this issue. Thank you. Here is the code snippet: <!DOCTYPE html> <html> <head> <style> </style> < ...

jQuery's utilization

I have this specific HTML element structure: <div class="myclassname" data-barcolor="#000000"></div> My goal is to retrieve the "data-barcolor" attribute using the (this) keyword in jQuery, rather than relying on the classname as shown below: ...

Ensure that all images uploaded are adjusted to fit the consistent dimensions within a card

I am in the process of creating a form that allows users to upload various images, which will then be displayed as cards on a website. After uploading an image, the size of the card automatically adjusts to fit the image, ensuring that all cards maintain ...

basic fading javascript image rotator

While looking for a simple fade image rotator, I stumbled upon this old post: Simple fade javascript image rotator request I finally found what I was searching for: $(function() { $('#fader img:not(:first)').hide(); $('#fader img').c ...

Implementing manual updates for the bootstrap color picker

I have integrated a color picker from Bootstrap into my project. You can find the color picker here: To change the background color of an element on my page, I am using the following code: <input type="text" id="mypicker" name="mypicker" value="" cla ...

Capture images with html2canvas in HTML

I am currently working on converting HTML to an image using html2canvas. I have encountered an issue where Arabic characters are not being converted correctly! </html> <body dir="rtl"> <a class="STO_one" href="#"> <span& ...

Navigating the complexities of Ajax and progressive enhancement: a helpful guide

I am creating a website using CakePHP and jQuery that I want to make ajax-friendly while ensuring accessibility for all users. I am exploring progressive enhancement to achieve this goal. In my beforeRender() function, I have a simple code snippet that is ...

Creating a layout where three divs are lined up next to each other, each with a height that spans

I am currently designing a website layout and I am working on setting up the CSS rules for the layout as shown in the image below: My goal is to have the left and right divs extend to the height of the content div. However, I am not very familiar with the ...