Home > Web Front-end > JS Tutorial > body text

How to Prevent Bootstrap Dropdowns from Closing on Click Inside?

Linda Hamilton
Release: 2024-10-31 19:22:30
Original
644 people have browsed it

How to Prevent Bootstrap Dropdowns from Closing on Click Inside?

Avoid Dropdown Menu Close on Click Inside

In Bootstrap dropdowns, menu items default to closing upon any click, even those contained within the dropdown itself. While attaching a click event handler to the dropdown menu with event.stopPropagation() resolves this issue, it can lead to problems with delegated events for carousel controls.

Solution:

To handle this dilemma, a more precise event delegation approach is required:

<code class="js">$(document).on('click', 'dropdown .dropdown-menu', function (e) {
  e.stopPropagation();
});</code>
Copy after login

This delegates the click event to the desired container, ensuring that clicks within it don't trigger the dropdown's closing.

The above is the detailed content of How to Prevent Bootstrap Dropdowns from Closing on Click Inside?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!