Bootstrap Popovers

bootstrap

What are popovers?

It is in-page popups / modal dialogs.

What are the requirements for using popovers?

Popovers require the tooltip plugin to be included in your version of Bootstrap.

Why do we have to specify container: 'body' when using tooltips or popovers on elements within an .input-group?

When using tooltips or popovers on elements within an .input-group, you'll have to specify the option container: 'body' to avoid unwanted side effects (such as the element growing wider and/or losing its rounded corners when the tooltip or popover is triggered).

Why do we have to initialize popovers explicitly?

For performance reasons, the Tooltip and Popover data-apis are opt-in, meaning you must initialize them yourself. One way to initialize all popovers on a page would be to select them by their data-toggle attribute:

$(function () {
  $('[data-toggle="popover"]').popover()
})

Can we have popovers in button groups or input groups?

Popovers in button groups and input groups require special setting. When using popovers on elements within a .btn-group or an .input-group, you'll have to specify the option container: 'body' to avoid unwanted side effects (such as the element growing wider and/or losing its rounded corners when the popover is triggered).

How can we add popovers for disabled elements?

Popovers on disabled elements require wrapper elements. To add a popover to a disabled or .disabled element, put the element inside of a <div> and apply the popover to that <div> instead.

What do we have to do when adding popovers for multi-line links?

Sometimes you want to add a popover to a hyperlink that wraps multiple lines. The default behavior of the popover plugin is to center it horizontally and vertically. Add white-space: nowrap; to your anchors to avoid this.

How can we add a popover for a button using the data attributes?

<button type="button" class="btn btn-default" 
    data-container="body" 
    data-toggle="popover" 
    data-placement="left" 
    data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
  Popover on left
</button>

How can we make a popover that dismiss on the next click?

Use the focus trigger to dismiss popovers on the next click. For proper cross-browser and cross-platform behavior, you must use the <a> tag, not the <button> tag, and you also must include the role="button" and tabindex attributes.

<a tabindex="0" class="btn btn-lg btn-danger" 
    role="button" 
    data-toggle="popover" 
    data-trigger="focus" 
    title="Dismissible popover" 
    data-content="And here's some amazing content. It's very engaging. Right?">
        Dismissible popover
</a>

How can we add a popover using JavaScript?

$('#example').popover(options)

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-animation="".

Name Type Default Description
animation boolean true Apply a CSS fade transition to the popover
container string or false false Appends the popover to a specific element. Example: container: 'body'. This option is particularly useful in that it allows you to position the popover in the flow of the document near the triggering element - which will prevent the popover from floating away from the triggering element during a window resize.
content string or function '' Default content value if data-content attribute isn't present. If a function is given, it will be called with its this reference set to the element that the popover is attached to.
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content
cell-content cell-content cell-content cell-content

See http://getbootstrap.com/javascript/#popovers

How can we initialize a popover?

$().popover(options)

How can we reveal an element's popover?

.popover('show')

Returns to the caller before the popover has actually been shown (i.e. before the shown.bs.popover event occurs). This is considered a "manual" triggering of the popover.

$('#element').popover('show')

How can we hide a popover?

.popover('hide')

Hides an element's popover. Returns to the caller before the popover has actually been hidden (i.e. before the hidden.bs.popover event occurs). This is considered a "manual" triggering of the popover.

$('#element').popover('hide')

How can we toggle a popover?

.popover('toggle')

Toggles an element's popover. Returns to the caller before the popover has actually been shown or hidden (i.e. before the shown.bs.popover or hidden.bs.popover event occurs).

$('#element').popover('toggle')

How can we destroy a popover?

.popover('destroy')

Hides and destroys an element's popover. Popovers that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements.

$('#element').popover('destroy')

What are the events used by popovers?

Event Type Description
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).
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.
$('#myPopover').on('hidden.bs.popover', function () {
  // do something…
})
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License