Ways to manually conceal a popover using Bootstrap 5

I am attempting to generate multiple Bootstrap popover instances that can all be closed with a single click.

Here is the approach I took:

document.addEventListener('DOMContentLoaded', function () {

    var items = [];

    document.getElementById('addPopperButton', function () {
        var btn = document.createElement('button');
        btn.setAttribute('type', 'button');
        btn.innerText = 'Toggle popover';

        var item = new bootstrap.Popover(btn, {
            trigger: 'hover click',
            title: 'lorem text',
            content: 'lorem text'
        });

        items.push(item);

        document.body.appendChild(btn);
    });

    document.getElementById('hideAllPopovers', function () {
        items.forEach(item => {
            item.popover('hide');
        });
    });

});

Despite my efforts, I keep encountering the following error:

Uncaught TypeError: item.popover is not a function

Is there a way for me to manually close all popover instances when the hideAllPopovers button is clicked?

Answer №1

To hide a popover in Bootstrap 5, you can utilize the hide method as shown below:

const popoverInstance = new bootstrap.Popover(triggerElement, options);

// Manually hide popover
popoverInstance.hide()

Prior to Bootstrap v5, $().popover('hide') was the method used.

In your code, if item represents a popover instance, replace item.popover('hide'); with item.hide();.

document.getElementById('hideAllPopovers').addEventListener('click', () => {
  items.forEach((item) => {
    item.hide();
  });
});

Check out this Stackblitz demo for reference: https://stackblitz.com/edit/stackblitz-starters-chdxor?file=src%2Findex.js


According to the Bootstrap 5 docs, the hide method:

Hides the popover element. Note that this method returns before the popover is actually hidden (i.e., before the hidden.bs.popover event occurs). This can be considered as a manual triggering of the popover hide action.

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

The inserted button's click event does not trigger the contentEditable DIV

I have a contentEditable DIV that I manage using the directive below: <div class="msg-input-area" [class.focused]="isMsgAreaFocused" contenteditable [contenteditableModel]="msgText" (contenteditableModelChang ...

Tips on inserting a script prior to the body tag

It appears to be a straightforward process, but I am struggling to figure out what I am missing: var string1 = document.createElement('script'); $(string1).attr('type', 'text/javascript'); $('body').before($(string ...

Ajax-powered Datatables

I am a beginner to data tables and I am attempting to retrieve data from a JSON text file (test1.txt). Below is an excerpt of the data present in the file, which contains over 5000 entries: [{"0":"22352442","ID":"22352442","1":"22126303","PARENT":"2212630 ...

In what specific format does this plugin expect the callback string to be encoded as? (jquery-ui-multisearch)

Currently, I'm working with a plugin named "Jquery-ui-multisearch" that provides an autocompletion feature in an input field based on either a provided array or an external source (such as ajax/api, etc). You can find more information about this plugi ...

initiate a click event within another click event

This is an HTML Code snippet: <ul id="linkjess"> <li><a href="javascript:setfolder('medewerkers', '1000');">medewerkers 1000</a></li> <li><a href="javascript:setfolder('medewerkers', ...

How to upload a document to Alfresco using JavaScript API

I am facing an issue while attempting to upload a file from a node application to my local Alfresco server. I have been able to login, create, and delete folders successfully, but I am encountering difficulties when trying to upload files. let AlfrescoApi ...

javascript dynamic content remains unaffected by ajax call

I'm a beginner with javascript and I am using a PHP variable to create links dynamically. Here is an example of how the variable is set: $addlink = '<button class="blueBtn btnSmall" id="current'.$product_id.'" onClick=addcart(' ...

How can we update a class attribute of a form field in Django to show error messages using Bootstrap 5?

Utilizing server-side validation with Bootstrap 5 requires the addition of the .is-invalid class to the input form field containing an error, in order to display it within a div having the class="invalid-feedback". In Django, updating the class a ...

Inserting multiple rows in MySql using JavaScript through a URL pathUnique: "

Hello there! I am currently attempting to send an array of objects to my MySql Database via an API endpoint. Below is the code snippet from my API: app.get("/orderdetails/add", (req, res) => { const { item__, Qty_Ordered, Unit_Price, ...

Utilizing the dynamic pairing of Three.js and CSS3 to bring life to animated 3D objects

I am currently working on a project that involves creating 3D Lego elements and assembling them into a figure using animation. I have successfully modeled the elements in Blender and exported them as .obj/mlt files without any issues. However, when it come ...

Transforming a menu tab into an active anchor when navigating to a particular section

Hello, I have successfully created a JavaScript code that can dynamically add menu tabs into the menu list window.addEventListener("load", () => { const navList = document.getElementById("nav_list") const fragment ...

The escape character appears to be misunderstood, causing an error due to an invalid escape sequence

When using the following syntax: executeJSCommand("location.href='StatusDetails.php?CHLD=1\&JNum=1024&JTitle=';"); An error occurs displaying: Invalid Escape Sequence ...

Converting RSS title to HTML format with the help of JavaScript

I am currently working on populating a menu on a webpage with the 5 latest topics from our site's RSS newsfeed using JavaScript (specifically jQuery version 1.9.1). I have been searching for a solution, but most answers I find reference deprecated scr ...

Transforming the select boxes into Twitter Bootstrap will require adjusting the jQuery change function to accommodate the

http://jsfiddle.net/xuAQv/ <-- Visit my code on JsFiddle to see the problem. I came across a piece of code that transforms regular form select boxes into styled versions using Twitter Bootstrap. The code simply hides the select box and generates the H ...

Error: Import statement is not allowed outside of module scope

I've been through multiple documentations and sources, but I'm still struggling to fix this error. I recently started learning JavaScript and decided to experiment with fetch() commands, however, I keep encountering an issue (the title). Below i ...

What methods are available for populating bootstrap columns dynamically with the use of jquery, php and smarty templating?

I am currently working on developing a dynamic news article module that needs to resemble the layout of a traditional newspaper article. The height of the news article must be restricted based on the size of the current view port. All information for the a ...

Issue with multiple dropdowns not functioning in Bootstrap 5

Before you criticize my imports, here are the scripts I am using: <script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script ...

Move the option from one box to another in jQuery and retain its value

Hey guys, I need some assistance with a jQuery function. The first set of boxes works perfectly with the left and right buttons, but the second set is not functioning properly and doesn't display its price value. I want to fix it so that when I click ...

Mastering the art of writing protractor scenarios

In a hypothetical scenario where an Angular app consists of two pages - one for contacts (featuring a table with contacts and an "add new contact" button) and another for adding a new contact, the following steps can be outlined: Click on the "add" butto ...

Initially, my PDF file does not get selected, except in the Internet Explorer browser

I am currently facing an issue with selecting PDF files in Internet Explorer. The process works smoothly in Google Chrome, but I encounter a problem when trying to select PDFs in IE. Specifically, when I attempt to select a PDF for the first time in Inter ...