Repeatedly tapping the Bootstrap Dropdown option

I'm having some trouble with my dropdown menu. When I click on the button to open it, everything works fine. But when I try to close it by clicking again, the menu glitches and sometimes won't close at all. Here's the HTML code I'm using:

<div class="col-md-4 dropdown"><h2 class="dd_text">Vegetables</h2>
                    <div class="btn-group" id="mydropdown">
                        <button style="font-size:24px" class="btn btn-light btn-secondary" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><img align="left" src="vegetables.jpg" class="image_on_dd"></img></button>
                            <div class="dropdown-menu force-scroll" aria-labelledby="dropdownMenuButton">
                                <label class="pure-material-checkbox">
                                <input type="checkbox" name="brinjal" value="brinjal"/><span>Brinjal</span></label>
                                <label class="pure-material-checkbox">
                                <input type="checkbox" name="potato" value="potato"/><span>Potato</span></label>
                                <label class="pure-material-checkbox">
                                <input type="checkbox" name="tomato" value="tomato"/><span>Tomato</span></label>
                                <label class="pure-material-checkbox">
                                <input type="checkbox" name="capsicum" value="capsicum"/><span>Capsicum</span></label>
                                <label class="pure-material-checkbox">
                                <input type="checkbox" name="onion" value="onion"/><span>Onion</span></label>

Here is my CSS code:

       .btn{
          margin-left:30px;
          background: white;
          border-color:white;
          width:300px;
          }

        .btn:hover{
          background: white;
          border-color:white;
         }

        .dropdown {
                position: relative;
                display: inline-block;
                }
        .dropdown-menu{
            width:100%;
        }

        .force-scroll{
          overflow-y: scroll;
          width:300px;
         }

         .dropdown-menu{
            transform: translate3d(0px, 85px, 0px)!important;
            }

What I'm getting as a result can be seen here: https://i.sstatic.net/Yy26Q.png

My ultimate goal is to have the dropdown menu close when I double-click the button and open when I single-click it. Is this achievable? Can anyone offer any guidance on how to fix this issue?

Answer №1

Your HTML code is not valid and there are several inactive rules in your CSS that need to be fixed. In addition, some bad practices are being used, such as using a Bootstrap button incorrectly to implement an icon as a button.

Here is a jsfiddle link with the corrected and optimized code. Check it out here

<div class="col-md-4 dropdown">
<h2 class="dd_text">Vegetables</h2>
<div class="btn-group" id="mydropdown">

<!-- toggle dropdown button -->
<button class="btn btn-light" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  <img src="https://cdn4.iconfinder.com/data/icons/miscellaneous-81-solid/128/salad_vegetable_plate_leaf_menu_diet_healthy_editable_food_fresh_fruits_green_-512.png" width="30" height="30" alt="vegetable">
</button>

<!-- dropdown menu -->
<div class="dropdown-menu force-scroll" aria-labelledby="dropdownMenuButton">
  <label class="pure-material-checkbox">
    <input type="checkbox" name="brinjal" value="brinjal" /><span>Brinjal</span>
  </label>
  <label class="pure-material-checkbox">
    <input type="checkbox" name="potato" value="potato" /><span>Potato</span>
  </label>
  <label class="pure-material-checkbox">
    <input type="checkbox" name="tomato" value="tomato" /><span>Tomato</span>
  </label>
  <label class="pure-material-checkbox">
    <input type="checkbox" name="capsicum" value="capsicum" /><span>Capsicum</span>
  </label>
  <label class="pure-material-checkbox">
    <input type="checkbox" name="onion" value="onion" /><span>Onion</span>
  </label>
</div>

Using double click on websites is not recommended as it is commonly used for selecting text or paragraphs. If you still wish to implement it, you can find a solution here.

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

Result box not displaying the expected JQuery UI Autocomplete results

Can someone assist me with Autocomplete? When I search for an RTO code, the result box appears but without a list (refer to Screen : 1). However, when I press the down arrow button on the keyboard, the list starts appearing one by one (as shown in Screen : ...

Conceal a div while revealing the other div

I am currently trying to achieve a unique visual effect. I have three divs named div1, div2, and div3. The objective is for div1 to slide up and disappear when clicked, while div2 simultaneously slides up and becomes visible. Similarly, when div2 is click ...

Creating a horizontal scrollbar in columns using Bootstrap can be accomplished by adjusting the CSS properties

Having a container created with boostrap: https://i.sstatic.net/bqAuf.png The question at hand is how to implement horizontal scrolling in order to maintain the aspect ratio of the initial image within the container on phone resolutions. Here is the con ...

What is the method for adding an event listener in AngularJS within an ng-repeat iteration?

I'm completely new to AngularJS and currently working on building a photo gallery. The basic idea is to have an initial page displaying thumbnail photos from Twitter and Instagram using an AngularJS ng-repeat loop. When a user hovers over an image, I ...

Automatically adjust the top margin of the content section to accommodate a fixed header height

After extensive searching, I've come across various solutions but none of them seem to fit my needs. I am a beginner/amateur developer. The issue I am facing is with a fixed header that resizes when scrolling. I understand that by adding padding-top: ...

Is it feasible to utilize Socket.io and Express in conjunction with templates without the need for routes?

Apologies for what might be considered a beginner question, but I'm curious if it's possible to run a Socket.io event server in one folder and have an HTML page utilize that server from a different folder. The reason for this question is because ...

Deleting sections of a string using JavaScript

I've encountered a rather unique issue where I need to address a bug on a website. The problem is that when a string is generated dynamically, it automatically adds 5 spaces before and after the string. Ideally, this should be fixed in the backend cod ...

Inject HTML entities, escaped for CSS, dynamically using JavaScript

My goal is to dynamically generate a list of HTMLElements with unique data-* attributes that correspond to various HTML Entities. These attributes will then be utilized by CSS to display content in pseudo elements like this: li:after { content: attr(dat ...

Refresh numerous pages

Currently, I'm working on a freelance project where I've been tasked with re-coding an outdated menu for my client. The website is built on static pages, so updating the menu manually on each page seems like a time-consuming and repetitive task. ...

What is the method for formatting within the <textarea> element?

While working on developing a comment system, I recently made the discovery that text areas cannot be formatted to retain paragraphs, line breaks, etc. This was particularly noticeable when comparing sites like StackOverflow, which uses a text editor inste ...

"Gone in a Scroll: Div vanishes as page moves

Fiddle: http://jsfiddle.net/ud6hejm6/ I was tasked with creating a website for a video game tournament. By opening the fiddle link provided, you can preview the page layout. A central div is featured in the design: <div class="middle teko" id="mezzo"& ...

Dynamically adjusting CSS Max-Height according to scroll position

Is there a CSS only technique to achieve the following scenario: An element is positioned absolutely at x,y coordinates on the screen. The document includes a vertical scrollbar depending on its content. Can the height of the element be adjusted based on ...

Is the displayed Cyrillic string in the incorrect character set?

I am facing an issue with extracting a value from a decoded JSON response obtained from a Russian API. The value within the JSON = Object268 Initially, it appeared as: Объект 268 After including <meta charset="utf-8"> in my html ...

Combining Multiple Partial Views with Their Respective View Models in ASP.NET MVC: A Step-by-Step Guide

I am working on a view named Register where I include other views using @Html.Partial("ViewName"). I am curious about how to call other ViewModels from the partial views within this main view. While I know that each ViewModel with its fields can be declar ...

What is the best way to align the action buttons on this webpage?

I'm struggling to center both buttons on the page. I've tried using margin:0 auto 0 auto; in the position property, but it doesn't seem to work. Strangely, when I used a table instead of a div, it worked. I'm curious to know why I can&a ...

What is the best way to set the background opacity to 0.7 without impacting the content within it?

Currently, I'm facing an issue with two divs - the outer one is identified as "BD" and the inner one as "content". Whenever I attempt to reduce the opacity of BD's background-color, the content inside also becomes less opaque including both image ...

Ensuring that there is no visual overlap or conflict between HTML inputs using Selenium with Python

Is it possible to use Selenium with Python to determine if HTML inputs are visually overlapping each other? For instance, the first image shows elements that overlap: https://i.sstatic.net/vsSUd.png While the second image displays elements that do not ov ...

What is the importance of including the source name when redirecting?

In a recent discussion (jQuery Ajax PHP redirecting to another page), it was mentioned that when utilizing ajax for redirection to a PHP page, an event needs to be set in the following manner: $.ajax({ type: "POST", url: "ajax.php", data: dataString, ...

Every click will nudge the square to the right

I have a square that should move 100px to the right when clicked, but on the next click it moves back to the left. How can I resolve this issue? On JSFiddle, the code works fine. However, when I try to run it in Chrome, I get an error message: Cannot re ...

Python Selenium encountering the StaleElementReferenceException bug

I'm encountering an issue with Selenium in Python that reads: selenium.common.exceptions.StaleElementReferenceException: Message: u'stale element reference: element is not attached to the page document\n Interestingly, this error occurs at ...