Hide popup using HTML when clicking outside of it

Currently, I have implemented Bootstrap Popover with HTML content using the code snippet below. This code is based on the answer provided at .

$(".song-status-link").popover({
            html: true,
            placement: 'bottom',
            content: function () {
                return $("#song-status-popover").html();
            }
});

In order to close the popover when clicking outside of it, I have included the following code:

$('html').on('mouseup', function (e) {
            if (!$(e.target).closest('.popover').length) {
                $('.popover').each(function () {
                    $(this).closest('div.popover').popover('hide');
                });
            }
});

Although the current setup works well, I am encountering an issue when trying to open the popover again after closing it by clicking outside. It requires two clicks on the link to open the popover again.

I am seeking assistance in identifying what might be missing or causing the popover not to open in a single click after being closed by an external click. Any help would be appreciated!

UPDATE: My theory is that even though clicking outside hides the popover, Bootstrap still registers it as open. Hence, on the first click after closure, it assumes the popover is already closed, taking a second click to actually open it.

Answer №1

Take a look at this code snippet

$('html').on('click', function(e) {
  if (!$(e.target).is('.song-status-link') && $(e.target).closest('.popover').length == 0) {
    $(".song-status-link").popover('hide');
  }
});

Explore the example on JSFiddle for more details

Answer №2

To locate it, you must use the .find() method and ensure that it is visible/available in the Document Object Model by using :visible:

if ($(this).find('.tooltip:visible').length) {
    $('.tooltip').tooltip('hide');
}

Answer №3

You can achieve this functionality using Bootstrap without any additional plugins. The only limitation is that the popover cannot contain focusable elements.

Refer to the Dismiss on next click section in the official documentation for more information.

This feature relies on the focus event to show the popover and the blur event to hide it. When you click outside of the trigger element, a blur event is triggered which hides the popover.

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

Is there a way to create an X shape by rotating two divs when I click on the container div?

I currently have this setup: code Below is the HTML code: <div id="box"> <div id="line1" class="line"></div> <div id="line2" class="line"></div> <div id="line3" class="line"></div> </div> My go ...

Subsequent modal forms do not trigger Ajax responses

I have a list of items displayed on a page, with each item having a link that opens a unique modal. Inside this modal, there is a text field for entering a username and an AJAX call to display usernames when "@" is typed (pretty cool, right?). The issue I& ...

I possess a single input field and I am seeking to have the focus shifted to it upon the activation of a button

Looking to enhance user input with an icon interaction: Clicking the icon should focus on the input Considering a solution for when clicking the icon to trigger focusout A code snippet has been implemented for the first requirement, seeking suggestions ...

Issue with vertical alignment within nested divs

I have been attempting to center a search bar inside a top banner vertically. My understanding was that the following code could achieve this: #banner { height: 35px; width: 100%; } #searchbar { height: 15px; position: relative; top: 50%; ...

Using jQuery and Bootstrap in an ASP.NET Core project

I am encountering an issue with the configuration of bootstrap and jquery within my project, causing these tools to fail to load properly. The problem seems to be that bootstrap is loading before jquery, resulting in error messages appearing when I check ...

Tips for verifying the contents of a textbox with the help of a Bootstrap

I am still learning javascript and I want to make a banner appear if the textbox is empty, but it's not working. How can I use bootstrap banners to create an alert? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&g ...

Tips for configuring scroll on Jquery Gantt Chart to focus on current date

Currently, I am utilizing the jQuery Gantt Chart. My goal is to have the default scroll position set to today's date. At the moment, it seems to start from the earliest recorded date instead of today's date. I would greatly appreciate any assista ...

Modify a value using jQuery

On my Asp.net webform, I have a label called "LabelTotalNumber". I am looking to update the value of this label every minute using jQuery without having to refresh the form. How can I achieve this? Currently, I am able to accomplish updating the value onc ...

issues with background image alignment in css

I attempted to add a sticky background to a div, which was successful. However, I am encountering an issue where the background does not stretch enough during scrolling. Below is the CSS code I used to apply the background, along with screenshots displayin ...

The responsiveness of Slick Slider's breakpoints is malfunctioning

After implementing a slider using slick slider, I encountered an issue with the width when testing it online and in a normal HTML file. If anyone could assist me in resolving this issue, I would greatly appreciate it. Please inspect the code for both scen ...

Animating SVG while scrolling on a one-page website

Is there a way to incorporate SVG animation scrolling in a single page website? I am inspired by websites like and . The first one stands out to me because the animation is controlled by scrollup and scrolldown actions. I haven't written any of my S ...

Store the sum of Jquery variables in MySQL database

I have a jquery function that calculates the sum of values from input checkboxes, and I'm looking to store this sum in a PHP variable. However, I am unsure of how to achieve this. Can anyone provide guidance? I am new to jquery and struggling with thi ...

Correct placement of elements using absolute positioning and optimized scroll functionality

I am currently working on implementing tooltips for items within a scrollable list. My goals for the tooltips are as follows: Ensure they are visible outside and not restricted by the scroll area Appear immediately after the corresponding item Be detach ...

How come the mouseover effect on the div remains even after the mouse has been removed?

How can I keep the original CSS class when the mouse moves away? function highlight( x, y) { var sel=document.getElementById(y); sel.style.borderBottom= "2px solid "+x; sel.style.opacity="1"; sel.style.transition="all eas ...

How to mimic the "show more" feature on Twitter without using ajax?

I have a list of 50 items that I want to display 10 at a time with a "More" link at the bottom. Can someone guide me on how to achieve this effect without using ajax? You can refer to this example for reference. Your help is much appreciated! UPDATE: Tha ...

Is it possible to modify the global CSS in react-router according to the different routes?

I am attempting to customize a global CSS class (.ant-content) on a per-route basis. I have experimented with importing CSS files that override .ant-content for specific React components loaded in different routes. However, the issue is that these CSS fi ...

Utilizing Jquery UI in Visual Studio 2012 for developing asp.net webforms applications

Recently upgraded to 2012 and encountering some difficulties with implementing a jQuery UI accordion in my default.aspx page. Here is the code snippet from my default.aspx page: <div id="accordian"> <div> <div> ...

Having trouble with your Ajax post request?

I am currently working on creating a form that allows users to input information and submit it without the page refreshing. The processing of the form data will occur after the user clicks the submit button. To achieve this, I am utilizing jQuery and Ajax ...

Having trouble with jQuery's scrollLeft function on elements that are not currently visible

Within a large container DIV that contains numerous other elements and has a scroll bar, an issue arises when determining the value of scrollLeft. When the DIV is visible, the scrollLeft() function returns the correct value, but when the element is hidden, ...

Is there a Unicode character substitution happening?

There are certain mobile browsers that appear to have trouble supporting all unicode characters, such as the down arrow icon like this: span.icon:after { content:"\25be"; } Currently, no symbol is shown. Is there a way for me to identify this i ...