Php

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,

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.

Related Post
<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

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories