Popover·续

Data attributes for individual popovers

Options for individual popovers can alternatively be specified through the use of data attributes, as explained above.

Using function with floatingConfig

const popover = new bootstrap.Popover(element, {
  floatingConfig(defaultBsFloatingConfig) {
    // const newFloatingConfig = {...}
    // use defaultBsFloatingConfig if needed...
    // return newFloatingConfig
  }
})

Methods

Method Description
disable Removes the ability for an element’s popover to be shown. The popover will only be able to be shown if it is re-enabled.
dispose Hides and destroys an element’s popover (Removes stored data on the DOM element). Popovers that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements.
enable Gives an element’s popover the ability to be shown. Popovers are enabled by default.
getInstance Static method which allows you to get the popover instance associated with a DOM element.
getOrCreateInstance Static method which allows you to get the popover instance associated with a DOM element, or create a new one in case it wasn’t initialized.
hide Hides an element’s popover. Returns a promise that resolves once the popover is hidden (i.e. after the hidden.bs.popover event fires). This is considered a “manual” triggering of the popover.
setContent Gives a way to change the popover’s content after its initialization.
show Reveals an element’s popover. Returns a promise that resolves once the popover is shown (i.e. after the shown.bs.popover event fires). This is considered a “manual” triggering of the popover. Popovers whose title and content are both zero-length are never displayed.
toggle Toggles an element’s popover. Returns a promise that resolves once the popover is shown or hidden (i.e. after the shown.bs.popover or hidden.bs.popover event fires). This is considered a “manual” triggering of the popover.
toggleEnabled Toggles the ability for an element’s popover to be shown or hidden.
update Updates the position of an element’s popover.
// getOrCreateInstance example
const popover = bootstrap.Popover.getOrCreateInstance('#example') // Returns a Bootstrap popover instance

// setContent example
popover.setContent({
  '.popover-header': 'another title',
  '.popover-body': 'another content'
})

The setContent method accepts an object argument, where each property-key is a valid string selector within the popover template, and each related property-value can be string | element | function | null

Events

Event Description
hide.bs.popover This event is fired immediately when the hide instance method has been called.
hidden.bs.popover This event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.bs.popover This event is fired after the show.bs.popover event when the popover template has been added to the DOM.
show.bs.popover This event fires immediately when the show instance method is called.
shown.bs.popover This event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete).
const myPopoverTrigger = document.getElementById('myPopover')
myPopoverTrigger.addEventListener('hidden.bs.popover', () => {
  // do something...
})