Skip to content

Instantly share code, notes, and snippets.

@mcarpenterjr
Last active September 26, 2017 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcarpenterjr/f47bd3aa5c3fe314674cbe1194d71246 to your computer and use it in GitHub Desktop.
Save mcarpenterjr/f47bd3aa5c3fe314674cbe1194d71246 to your computer and use it in GitHub Desktop.
small reusable function for toggling the visual state of bootstrap button groups, and returning a value based on the selected one. The idea is if it is checkbox we toggle its visual state by changing the background and changing the icon on the button. If it is a radio button group we just change the icon on the target button and reset the icon o…
self.cbTog = function(b) {
var self = this;
var gs = $(b).parent('label').siblings().length;
// console.log($(b).parent('label').siblings().length);
if (gs > 0) {
// This is a multi option item, mostlikely a radio.
$(b)
.siblings('i')
.addClass('fa-check-square-o')
.removeClass('fa-square-o')
.closest('label')
.css('padding-right', '3px')
.siblings()
.each(function() {
$(this)
.css('padding-right', '5px')
.find('i')
.filter('.fa-check-square-o')
.addClass('fa-square-o')
.removeClass('fa-check-square-o');
});
// return the value based on the type.
// In this case we have some id's that end with yes, no or na, if the target maches them we return 1, 2 or 3.
// If nothing matches we return null.
return $(b).is('[id$="yes"]') ? 1 : $(b).is('[id$="no"]') ? 2 : $(b).is('[id$="na"]') ? 3 : null;
}
else {
// This is a checkbox
if ($(b).is(':checked')) {
$(b)
.siblings('i')
.addClass('fa-square-o')
.removeClass('fa-check-square-o')
.closest('label')
.css('padding-right', '5px')
.addClass('btn-danger')
.removeClass('btn-success')
.filter('.btn-info')
.removeClass('btn-success btn-warning btn-danger');
return 0;
}
else {
$(b)
.siblings('i')
.addClass('fa-check-square-o')
.removeClass('fa-square-o')
.closest('label')
.css('padding-right', '3px')
.addClass('btn-success')
.removeClass('btn-danger btn-warning')
.filter('.btn-info')
.removeClass('btn-success btn-warning btn-danger');
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment