Skip to content

Instantly share code, notes, and snippets.

@gabriellett
Last active December 29, 2015 10:19
Show Gist options
  • Save gabriellett/7655997 to your computer and use it in GitHub Desktop.
Save gabriellett/7655997 to your computer and use it in GitHub Desktop.
Statefull bootstrap button
/*
Author: Gabriel Lett Viviani
Date: 26/11/2013
Email: lett.de@gmail.com
Buttons must have the data-class-active attribute, set it to the
class that you want added when the element is active. The class
'active' will be always added
Button groups must have the data-active-type set to radio if you
want it to behave as a radio button.
*/
// Get all elements that contain the 'data-class-active' attr
$(":button[data-class-active].btn").click(function() {
// Current button
var $ele = $j(this);
// Try to get the parent btn-group div
var btnGroup = $ele.parent('.btn-group');
// If the button is inside a group and the group has the data-active-type attr set to 'radio'
if(btnGroup != undefined && btnGroup.data("activeType") != undefined && btnGroup.data("activeType") == 'radio') {
// Deactivate all buttons except the current
btnGroup.find('.btn').each(function() {
if($ele.data('acao') != $(this).data('acao')){
$(this).removeClass($(this).data('classActive'));
$(this).removeClass('active');
}
});
}
// Activate or deactivate the current button
if($ele.hasClass("active")) {
$ele.removeClass($ele.data('classActive'));
$ele.removeClass('active');
} else {
$ele.addClass($ele.data('classActive'));
$ele.addClass('active');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment