Home >Web Front-end >JS Tutorial >How to Prevent Bootstrap Dropdowns from Closing on Click Inside?

How to Prevent Bootstrap Dropdowns from Closing on Click Inside?

Linda Hamilton
Linda HamiltonOriginal
2024-10-31 19:22:30761browse

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>

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!

Statement:
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
Previous article:IndexedDB explained.Next article:IndexedDB explained.