I have made a jsFiddle where i use twitter bootstrap toggle buttons & popover.
HTML:
<div class="btn-group btn-toggle">
<button class="btn btn-sm btn-info">On</button>
<button class="btn btn-sm btn-primary active">Off</button>
</div>
<button id="popbutton" class="btn btn-lg btn-warn">pop up</button>
JS:
var popupElement = '<div class="btn-group btn-toggle"><button class="btn btn-sm btn- info">On</button><button class="btn btn-sm btn-primary active">Off</button></div>';
$('#popbutton').popover({
animation: true,
content: popupElement,
html: true
});
$('.btn-toggle').click(function () {
$(this).find('.btn').toggleClass('active');
if ($(this).find('.btn-primary').size() > 0) {
$(this).find('.btn').toggleClass('btn-primary');
}
if ($(this).find('.btn-danger').size() > 0) {
$(this).find('.btn').toggleClass('btn-danger');
}
if ($(this).find('.btn-success').size() > 0) {
$(this).find('.btn').toggleClass('btn-success');
}
if ($(this).find('.btn-info').size() > 0) {
$(this).find('.btn').toggleClass('btn-info');
}
$(this).find('.btn').toggleClass('btn-default');
});
The toggle button works, but when i use the same buttons inside a popover, it won't work.
Please suggest a method.
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.
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.
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.
To create a new popover with JavaScript, you can use the popover() function. To determine which days already have a meeting scheduled, we can add a custom data-booked property. The selector $('[data-booked="true"]') is an attribute selector and will only display a popover for that particular button.
Since your popover buttons do not exist at load time you need to use event delegation. Also instead of size()
(which is deprecated as of jQuery 1.8) you should use the length
property:
$('body').on('click','.btn-toggle',function () {
$(this).find('.btn').toggleClass('active');
if ($(this).find('.btn-primary').length > 0) {
$(this).find('.btn').toggleClass('btn-primary');
}
if ($(this).find('.btn-danger').length > 0) {
$(this).find('.btn').toggleClass('btn-danger');
}
if ($(this).find('.btn-success').length > 0) {
$(this).find('.btn').toggleClass('btn-success');
}
if ($(this).find('.btn-info').length > 0) {
$(this).find('.btn').toggleClass('btn-info');
}
$(this).find('.btn').toggleClass('btn-default');
});
Updated fiddle
this is how you should do it , if you gonna append new elements to your page:
$('body').on('click','.btn-toggle',function() {
$(this).find('.btn').toggleClass('active');
if ($(this).find('.btn-primary').length > 0) {
$(this).find('.btn').toggleClass('btn-primary');
}
if ($(this).find('.btn-danger').length > 0) {
$(this).find('.btn').toggleClass('btn-danger');
}
if ($(this).find('.btn-success').length > 0) {
$(this).find('.btn').toggleClass('btn-success');
}
if ($(this).find('.btn-info').length > 0) {
$(this).find('.btn').toggleClass('btn-info');
}
$(this).find('.btn').toggleClass('btn-default');
});
here is a fiddle:
http://jsfiddle.net/prollygeek/ZVak6/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With