Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set default property values for twitter bootstrap popover?

How to set the default value manually in twitter bootstrap 2 and twitter bootstrap 3?

I have the following code, you can see the same properties in the options being used over again (I have to overwrite them because they are required to be different from the defaults).

The three properties that are used over again are trigger placement & html.

How I can override the default options set by twitter bootstrap in my javascript?

Javascript (On DOM Ready)

$('.pop1').popover({
    trigger: 'hover',
    placement : 'right',
    html: true, // used once (and the 2 above)
    title : 'My title for popover1',
    content : 'My content with popover1'
});

$('.pop2').popover({
    trigger: 'hover', //used again
    placement : 'right',
    html: true,
    title : 'My title for popover2',
    content : 'My content with popover2'
});

$('.pop3').popover({
    trigger: 'hover', //and again!
    placement : 'right',
    html: true,
    title : 'My title for popover3',
    content : 'My content with popover3'
});

HTML:

<span class="pop1"><i class="icon-info-sign"></i></span>
<span class="pop2"><i class="icon-info-sign"></i></span>
<span class="pop3"><i class="icon-info-sign"></i></span>

I wanted something like jQuery validators (& other plugins): jQuery.validator.setDefaults()

So basically, how can I set the bootstrap default values for twitter bootstrap 2 and twitter bootstrap 3?, I would like it so bootstrap uses my custom default values so I don't have to override them over and over again throughout my code, How can I customize it so the default value is one specified by me and not Twitter Bootstrap 2 or Twitter Bootstrap 3.

like image 915
Anil Avatar asked Jul 11 '13 09:07

Anil


People also ask

How do I customize Bootstrap popover?

To create a popover, you need to add the data-bs-toggle="popover" attribute to an element. Whereas, popover's title and its content that would display upon trigger or activation can be specified using the title and data-bs-content attribute respectively. Here is the standard markup for adding a popover to a button.

How do I enable popovers in bootstrap?

To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.

How does bootstrap define popover?

A Bootstrap Popover is an attribute in bootstrap that can be used to make any website look more dynamic. Popovers are generally used to display additional information about any element and are displayed with a click of a mouse pointer over that element.


1 Answers

Bootstrap V2 - View the JSFiddle

You can log the defaults to see all the defaults set by Bootstrap 2 using:

console.log($.fn.popover.defaults); // Log default values to the console

You can override the defaults set by Twitter Bootstrap 2 using the following:

// Make sure jQuery is loaded before trying to override the defaults!
$.fn.popover.defaults.trigger = 'hover';   // Set the default trigger to hover
$.fn.popover.defaults.placement = 'right'; // Set the default placement to right
$.fn.popover.defaults.html = true;         // Set the default html (parse html) to true

View the JSFiddle here


Bootstrap V3 - View the JSFiddle

You can log the defaults to see all the defaults set by Bootstrap 3 using:

console.log($.fn.popover.Constructor.DEFAULTS); // Log default values to the console

You can override the defaults set by Twitter Bootstrap 3 using the following:

// Make sure jQuery is loaded before trying to override the defaults!
$.fn.popover.Constructor.DEFAULTS.trigger = 'hover';   // Set the default trigger to hover
$.fn.popover.Constructor.DEFAULTS.placement = 'right'; // Set the default placement to right
$.fn.popover.Constructor.DEFAULTS.html = true;         // Set the default html (parse html) to true

View the JSFiddle here


Bootstrap V4 - View the JSFiddle

The syntax varies slightly from Bootstrap 3:

$.fn.popover.Constructor.Default.html = true;

Default Values

By default the values are set to the following (they are the same in Boostrap 2 & 3):

animation: true
container: false
content: ""
delay: 0
html: false
placement: "right"
selector: false
template: "<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>"
title: ""
trigger: "hover"
like image 113
Anil Avatar answered Oct 13 '22 15:10

Anil