Bootstrap Popover : How to Create Awesome Dropdown

In this tutorial, I will describe how to use bootstrap popover for custom HTML control, normally we used popover to display tooltip information on help, but sometimes we need HTML control or HTML form in the popover. This tutorial helps you to create HTML control on popover.

The bootstrap popover is used to show information or HTML element. Bootstrap is a very popular CSS framework to create elegant UI within minutes. This Bootstrap tutorial help to create a beautiful popover using static information or HTML content.

You can use popover using data attribute or JavaScript. There are a lot of callback method and events available for bootstrap popover.

Also Checkout other Popup Box tutorial,

popover-dropdown

Option 1: Using data attribute

You can define popover on anchor/button element using data-toggle = "popover" attribute. The title of the anchor/button will be the text of a popover which will show. The default position of popover is top by the plugin.

<button class="btn btn-primary" title="Popover title" type="button" data-toggle="popover" data-placement="top" data-content="Default popover">Popover on top</button>

Where :

  • data-toggle : Use to activate the popover on the element.
  • title: Use for the title of the popover.
  • data-content : Use to display content inside the popover’s body.

Option 2: Using JavaScript

You can also initialize popover on the HTML element using the jquery selector. We can use HTML element Id/Class as a jQuery selector and call popover() method on it. The following code will enable popovers on all HTML elements which has data-toggle attribute.

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

Create HTML control With in Bootstrap Popover

Step 1: Include all necessary library into head section of index.php file.

<link rel="stylesheet" href="/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="/jquery/1.11.1/jquery.min.js"></script>
<script src="/bootstrap/3.2.0/js/bootstrap.min.js"></script>

Step 2: We need to define HTML layout.

<div id="popover1" class="col-sm-12 col-xs-12 col-md-9"><a class="btn btn-primary change-trigger" title="" data-original-title="popover Test">Popover Example</a>
<div id="select-div" class="hide">
<div class="col-sm-2 " style="width: 250px;"><select class="form-control">
<option>Test</option>
<option>Test1</option>
</select></div>
<div class="clearfix col-sm-10" style="margin: 8px 0;"><button class="btn btn-default btn-go" type="button">Go!</button> <button class="btn btn-default btn-cancel-option" type="button">Cancel</button></div>
</div>
</div>

Above code create a link button which will use to generate a popover on click. We have HTML select control with hide properties.

Step 3: Now we will call the bootstrap popover method.

$('.change-trigger').popover({
            placement : 'Right',
            title : 'Change',
            trigger : 'click',
            html : true,
            content : function(){
                var content = '';
				content = $('#select-div').html();
				return content;
            } 
        }).on('shown.bs.popover', function(){
        });

        $(document).delegate('.btn-go','click', function(e){
            e.preventDefault();
            alert('Go Click');
        });

        $(document).delegate('.btn-cancel-option', 'click', function(e){
            e.preventDefault();
            var element = $(this).parents('.popover');
            if(element.size()){
                $(element).removeClass('in').addClass('out');
            }
        });

Live Demo and Download Source Code

3 thoughts on “Bootstrap Popover : How to Create Awesome Dropdown

  1. Hi Parvez
    I am trying to place a drop down in body of popover and depending on selected value in dropdown I want to show data related to that selected option in the same body of the popover how can I achieve this……..

Leave a Reply

Your email address will not be published.