The persistent Bulma dropdown glitch that refuses to close

In the project I'm working on, I have implemented a Bulma dropdown. While the dropdown functions correctly, I am facing an issue when adding multiple dropdowns in different columns with backend integration. When one dropdown is open and another is clicked to open, the first one remains open causing the dropdowns to overlap.

The problem arises because the .is-active class assigned to indicate the open state of a dropdown does not remove the previous .is-active class when trying to open another dropdown.

Is there a solution to this issue?

View image description here

View image description here

$(document).ready(function() {
  // Get all dropdowns on the page that aren't hoverable.
  var dropdowns = document.querySelectorAll('.dropdown:not(.is-hoverable)');

  if (dropdowns.length > 0) {
    // For each dropdown, add event handler to open on click.
    dropdowns.forEach(function(el) {
      el.addEventListener('click', function(e) {
        if (!el.classList.contains("is-active")) {
          el.classList.toggle('is-active');
          e.stopPropagation();
          e.preventDefault();
        }
      });
    });

    // If user clicks outside dropdown, close it.
    document.addEventListener('click', function(e) {
      closeDropdowns();
    });
  }

  /*
   * Close dropdowns by removing `is-active` class.
   */
  function closeDropdowns() {
    dropdowns.forEach(function(el) {
      el.classList.remove('is-active');
    });
  }

  // Close dropdowns if ESC pressed
  document.addEventListener('keydown', function(event) {
    let e = event || window.event;
    if (e.key === 'Esc' || e.key === 'Escape') {
      closeDropdowns();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dropdown is-right ">
  <div class="dropdown-trigger">
    <button class="button" aria-haspopup="true" aria-controls="dropdown-menu3">
      <span><strong class="fw900">. . .</strong></span>
    </button>
  </div>
  <div class="dropdown-menu" id="dropdown-menu3" role="menu">
    <div class="dropdown-content">
      <a href="javascript:void(0);" class="dropdown-item" onclick="">Delete</a>
      <hr class="dropdown-divider">
      <a href="javascript:void(0);" class="dropdown-item" onclick="">Edit</a>
      <hr class="dropdown-divider">
      <a href="javascript:void(0);" class="dropdown-item" onclick="">Approval Status</a>
    </div>
  </div>
</div>

Answer №1

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dropdown is-right ">
  <div class="dropdown-trigger">
    <button class="button" aria-haspopup="true" aria-controls="dropdown-menu3">
      <span><strong class="fw900">. . .</strong></span>
    </button>
  </div>
  <div class="dropdown-menu" id="dropdown-menu3" role="menu">
    <div class="dropdown-content">
      <a href="javascript:void(0);" class="dropdown-item" onclick="">Sil</a>
      <hr class="dropdown-divider">
      <a href="javascript:void(0);" class="dropdown-item" onclick="">Duzenle</a>
      <hr class="dropdown-divider">
      <a href="javascript:void(0);" class="dropdown-item" onclick="">OnayDurumu</a>
    </div>
  </div>
</div>




$(document).ready(function() {
  // Locate all dropdown elements that do not support hover.
  var selectDropdowns = document.querySelectorAll('.dropdown:not(.is-hoverable)');

  if (selectDropdowns.length > 0) {
    // Cycle through each dropdown to trigger opening upon click.
    selectDropdowns.forEach(function(element) {
      element.addEventListener('click', function(e) {
      $(".dropdown-menu").toggle();
        if (!element.classList.contains("is-active")) {
          element.classList.toggle('is-active');
          
          e.stopPropagation();
          e.preventDefault();
        }
      });
    });

    // Close any open dropdown when clicking outside.
    document.addEventListener('click', function(e) {
      closeDropdowns();
    });
  }

  /*
   * Remove `is-active` class to close dropdown menus.
   */
  function closeDropdowns() {
    selectDropdowns.forEach(function(element) {
      element.classList.remove('is-active');
    });
  }

  // Close dropdowns on ESC key press
  document.addEventListener('keydown', function(event) {
    let e = event || window.event;
    if (e.key === 'Esc' || e.key === 'Escape') {
      closeDropdowns();
    }
  });
});

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

Making jQuery / Javascript delay until the php script has fully loaded

I am encountering an issue with an ajax request that needs to wait for a PHP script to load. The code works fine in Chrome but not in IE, where the alert box displays too quickly and the script does not run at all. Upon pressing a button, the script shoul ...

Apply a specific CSS class to a division depending on the currently active cookie

I have implemented cookies on specific pages using https://github.com/carhartl/jquery-cookie. Could someone guide me on how to apply a CSS class to an ID (e.g., adding class .red to #mouse if the cookie named "socks" is active)? For example: If there is ...

What is the best way to notify the user about the input in the textbox?

Imagine you have a button and an input field. How could you notify the user of what is in the input field when the button is pressed? Please provide a simple explanation of your code. ...

Setting the color for the :hover and :active states on a react JSX component: A step-by-step guide

<button onClick={fetchExchangeRates} style={{ backgroundColor: `${selectedColor}` }} className="hover text-white px-3 py-1 flex justify-center items-center gap-2 text- rounded-md" > Convert <FontAwesomeIcon className=&apo ...

Troubleshooting Angular 2 Fallback Route Failure

My current project is using Angular 2 Webpack Starter but I am having trouble with the fallback route. In my app.routes.ts file, I have defined the routes as follows: import { Routes } from '@angular/router'; import { HomeComponent } from &apos ...

Seeking guidance on accessing and sending PHP variables using jQuery?

i am working with some PHP variables: $value1 = 12345; $value2 = "value2"; $value3 = "value3"; and there is a jQuery function: onaction:function(response){ $('input#div').trigger('click'); } my goal is to send these PHP vars ...

Looking for a Hack to Integrate a Music Player into Your Website?

Currently, I am utilizing the Jplayer plugin from JQuery to incorporate an Audio player into my website. I have come across a situation where: If the user is not currently listening to any music while browsing the website, the page can load without any i ...

What is the process for associating JSON reponses with button actions on a webpage?

I developed a JavaScript script that interacts with a Tableau server API to handle running jobs. The script presents the retrieved jobs on a web page, along with corresponding buttons that enable users to terminate specific jobs. While the script function ...

JQuery Load fails to load in real-time

I have a PHP file where I am using JQuery to load the content of another file called 'live.php' which should display the current time, but for some reason it is not loading live. Can anyone help me troubleshoot this issue? It used to work perfect ...

ngSanitize continues to present plain text rather than rendering HTML code

When working with AngularJS scope, I encountered the need to display certain items as HTML. After some research, I realized that integrating ngSanitize was necessary for this functionality. Here is how I implemented it in my app: <script src="Scripts/a ...

What is the best way to modify React state?

Can someone help me troubleshoot an issue with toggling React state after a button click? I want to be able to change "Work From Office" to "Work From Home" and vice versa, but it's only working once. Is there a way to achieve this using an if stateme ...

Is there a way to retrieve the request object within loopback operational hooks?

I am currently utilizing loopback 3.x and have created an Access Hook within my code. My goal is to include a condition based on the User Agent. Specifically, I am looking to access Request > Headers > User-Agent. Is it feasible to retrieve this in ...

Is there a way to get rid of the tiny black line beside the Instagram icon?

Here is the display on my website This is the code that was used I am struggling to identify the source of the small black "-" line next to the Instagram icon logo. ...

Javascript code requires server to have default text field values

Based on the choice made by a user, a text field box will either remain concealed or revealed: $("#aForm").on("change", function() { if ($(this).val() == "a") $("#textField").hide(); else $("#textField").show(); }); The issue arises when the ...

Encountering an error when attempting to include React TypeScript onChange with a Material UI switch component

I'm working on implementing a show/hide functionality using a switch. I want the component to be displayed when the switch is turned on and hidden when it's turned off. Here's the code I've written: const VirtualEventSection = ({ con ...

Creating a stunning image reveal animation using GSAP and clip-path within a React environment

When attempting to create an animation for revealing an image using GSAP and the clip-path property within a Component, I encountered an issue. The animation works perfectly when using the first clip-path value, but as soon as I switch to the other one, th ...

After updating the External SS, the background vanishes

There appears to be an issue with a page that I designed. I originally wrote the CSS code internally within the <style type="text/css>...</style> section, including a background image, and all was working fine initially. However, after transf ...

Problems with adjusting image sizes on different devices

img { width: 100%; height: auto; } The above code adjusts the width of the images to match the width of the page, while maintaining the original aspect ratio with the height. However, if we were to use the following: img { width: auto; h ...

Using a JQuery function to execute when a specific radio button is clicked and also on page load

Just diving into JQuery and ran into a little issue with my function. I'm attempting to tweak it so that it only triggers when a specific radio button is clicked (I thought '#UpdateType input[type="radio"]' would do the trick) and also runs ...

Prevent users from adding or removing any letters

Exploring the ACE Editor integration with AngularJS (utilizing UI-Ace). I have a question. Is it possible to limit the user's actions to: Only allow entering a predefined character (for example, ;) Prevent deletion of any characters except for the p ...