Bootstrap – Popover Plugin

Bootstrap Popover Plugin

In this guide, we will discuss Popover plugin in Bootstrap. The popover is similar to tooltip, offering an extended view complete with a heading. For the popover to activate, a user just needs to hover the cursor over the element. The content of the popover can be populated entirely using the Bootstrap Data API. This method requires a tooltip.

If you want to include this plugin functionality individually, then you will need the popover.js and it has a dependency of the tooltip plugin. Else, as mentioned in the chapter Bootstrap Plugins Overview, you can include bootstrap.js or the minified bootstrap.min.js.

Usage

The popover plugin generates content and markup on demand, and by default places popover after their trigger element. You can add popover in the following two ways βˆ’

  • Via data attributes βˆ’ To add a popover, add data-toggle = “popover” to an anchor/button tag. The title of the anchor will be the text of a popover. By default, popover is set to top by the plugin.
<a href = "#" data-toggle = "popover" title = "Example popover">
   Hover over me
</a>
  • Via JavaScript βˆ’ Enable popovers via JavaScript using the following syntax βˆ’
$('#identifier').popover(options)

Popover plugin is NOT only-css plugins like dropdown or other plugins discussed in previous chapters. To use this plugin you MUST activate it using jquery (read javascript). To enable all the popovers on your page just use this script βˆ’

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

Example

The following example demonstrates the use of popover plugin via data attributes.

<div class = "container" style = "padding: 100px 50px 10px;" >
   <button type = "button" class = "btn btn-default" title = "Popover title"  
      data-container = "body" data-toggle = "popover" data-placement = "left" 
      data-content = "Some content in Popover on left">
      
      Popover on left
   </button>
   
   <button type = "button" class = "btn btn-primary" title = "Popover title"  
      data-container = "body" data-toggle = "popover" data-placement = "top" 
      data-content = "Some content in Popover on top">
      
      Popover on top
   </button>
   
   <button type = "button" class = "btn btn-success" title = "Popover title"  
      data-container = "body" data-toggle = "popover" data-placement = "bottom" 
      data-content = "Some content in Popover on bottom">
      
      Popover on bottom
   </button>
   
   <button type = "button" class = "btn btn-warning" title = "Popover title"  
      data-container = "body" data-toggle = "popover" data-placement = "right" 
      data-content = "Some content in Popover on right">
      
      Popover on right
   </button>
   
</div>

<script>
   $(function (){
      $("[data-toggle = 'popover']").popover();
   });
</script>

Options

There are certain options which can be added via the Bootstrap Data API or invoked via JavaScript. Following table lists the options βˆ’

Option NameType/Default ValueData attribute nameDescription
animationboolean Default βˆ’ truedata-animationApplies a CSS fade transition to the popover.
htmlboolean Default βˆ’ falsedata-htmlInserts HTML into the popover. If false, jQuery’s text method will be used to insert content into the dom. Use text if you’re worried about XSS attacks.
placementstring|function Default βˆ’ topdata-placementSpecifies how to position the popover (i.e., top|bottom|left|right|auto). When auto is specified, it will dynamically reorient the popover. For example, if placement is “auto left”, the popover will display to the left when possible, otherwise it will display right.
selectorstring Default βˆ’ falsedata-selectorIf a selector is provided, popover objects will be delegated to the specified targets.
titlestring | function Default βˆ’ “data-titleThe title option is the default title value if the title attribute isn’t present.
triggerstring Default βˆ’ ‘hover focus’data-triggerDefines how the popover is triggered βˆ’ click| hover | focus | manual. You may pass multiple triggers; separate them with a space.
delaynumber | object Default βˆ’ 0data-delayDelays showing and hiding the popover in ms β€” does not apply to manual trigger type. If a number is supplied, delay is applied to both hide/show. Object structure is βˆ’delay: { show: 500, hide: 100 }
containerstring | false Default βˆ’ falsedata-containerAppends the popover to a specific element. Example: container: ‘body’

Methods

The following are some useful methods for popover βˆ’

MethodDescriptionExample
Options βˆ’ .popover(options)Attaches a popover handler to an element collection.$().popover(options)
Toggle βˆ’ .popover(‘toggle’)Toggles an element’s popover.$(‘#element’).popover(‘toggle’)
Show βˆ’ .popover(‘show’)Reveals an element’s popover.$(‘#element’).popover(‘show’)
Hide βˆ’ .popover(‘hide’)Hides an element’s popover.$(‘#element’).popover(‘hide’)
Destroy βˆ’ .popover(‘destroy’)Hides and destroys an element’s popover.$(‘#element’).popover(‘destroy’)

Example

The following example demonstrates the popover plugin methods βˆ’

<div class = "container" style = "padding: 100px 50px 10px;" >
   <button type = "button" class = "btn btn-default popover-show" 
      title = "Popover title" data-container = "body" 
      data-toggle = "popover" data-placement = "left" 
      data-content = "Some content in Popover with show method">
      
      Popover on left
   </button>
   
   <button type = "button" class = "btn btn-primary popover-hide" 
      title = "Popover title" data-container = "body" 
      data-toggle = "popover" data-placement = "top" 
      data-content = "Some content in Popover-hide method">
      
      Popover on top
   </button>
   
   <button type = "button" class = "btn btn-success popover-destroy" 
      title = "Popover title" data-container = "body" 
      data-toggle = "popover" data-placement = "bottom" 
      data-content = "Some content in Popover-destroy method">
      
      Popover on bottom
   </button>
   
   <button type = "button" class = "btn btn-warning popover-toggle" 
      title = "Popover title" data-container = "body" 
      data-toggle = "popover" data-placement = "top" 
      data-content = "Some content in Popover-toggle method">
      
      Popover on right
   </button>
	
   <br><br><br><br><br><br>
   
   <p class = "popover-options">
      <a href = "#" type = "button" class = "btn btn-warning" 
         title = "<h2>Title</h2>" data-container = "body" 
         data-toggle = "popover" data-content = "
         <h4>Some content in Popover-options method</h4>">
         
         Popover
      </a>
   </p>
   
   <script>
      $(function () { $('.popover-show').popover('show');});
      $(function () { $('.popover-hide').popover('hide');});
      $(function () { $('.popover-destroy').popover('destroy');});
      $(function () { $('.popover-toggle').popover('toggle');});
      $(function () { $(".popover-options a").popover({html : true });});
   </script>
	
</div>

Events

Following table lists the events to work with the popover plugin. This event may be used to hook into the function.

EventDescriptionExample
show.bs.popoverThis event fires immediately when the show instance method is called.$(‘#mypopover’).on(‘show.bs.popover’, function () { // do something })
shown.bs.popoverThis event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete).$(‘#mypopover’).on(‘shown.bs.popover’, function () { // do something })
hide.bs.popoverThis event is fired immediately when the hide instance method has been called.$(‘#mypopover’).on(‘hide.bs.popover’, function () { // do something })
hidden.bs.popoverThis event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete).$(‘#mypopover’).on(‘hidden.bs.popover’, function () { // do something })

Example

The following example demonstrates the Popover plugin events βˆ’

<div clas = "container" style = "padding: 100px 50px 10px;" >
   <button type = "button" class = "btn btn-primary popover-show" 
      title = "Popover title" data-container = "body" data-toggle = "popover" 
      data-content = "Some content in Popover with show method">
      
      Popover on left
   </button>
   
</div>

<script>
   $(function () { $('.popover-show').popover('show');});
   
   $(function () { $('.popover-show').on('shown.bs.popover', function () {
      alert("Alert message on show");
   })});
</script>

Next Topic : Click Here

Leave a Reply