"jQuery Checkbox Buttons"
Bootstrap 3.0.3 Snippet by travislaynewilson

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <h3>jQuery Checkbox Buttons<br /> <small>Buttons that change the state of their own hidden checkboxes, and vice-versa!</small> </h3> <br /> <span class="button-checkbox"> <button type="button" class="btn" data-color="primary">Unchecked</button> <input type="checkbox" class="hidden" /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="primary">Checked</button> <input type="checkbox" class="hidden" checked /> </span> <hr /> <!-- All colors --> <span class="button-checkbox"> <button type="button" class="btn" data-color="default">Default</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="primary">Primary</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="success">Success</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="info">Info</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="warning">Warning</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="danger">Danger</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="link">Link</button> <input type="checkbox" class="hidden" checked /> </span> <hr /> <!-- All sizes --> <span class="button-checkbox"> <button type="button" class="btn btn-xs" data-color="primary">Primary</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn btn-sm" data-color="primary">Primary</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="primary">Primary</button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn btn-lg" data-color="primary">Primary</button> <input type="checkbox" class="hidden" checked /> </span> <hr /> <!-- Icons --> <span class="button-checkbox"> <button type="button" class="btn" data-color="primary"><i class="glyphicon glyphicon-envelope"></i></button> <input type="checkbox" class="hidden" checked /> </span> <span class="button-checkbox"> <button type="button" class="btn" data-color="primary"><i class="glyphicon glyphicon-phone"></i></button> <input type="checkbox" class="hidden" /> </span> </div>
$(function () { $('.button-checkbox').each(function () { // Settings var $widget = $(this), $button = $widget.find('button'), $checkbox = $widget.find('input:checkbox'), color = $button.data('color'), settings = { on: { icon: 'glyphicon glyphicon-check' }, off: { icon: 'glyphicon glyphicon-unchecked' } }; // Event Handlers $button.on('click', function () { $checkbox.prop('checked', !$checkbox.is(':checked')); $checkbox.triggerHandler('change'); updateDisplay(); }); $checkbox.on('change', function () { updateDisplay(); }); // Actions function updateDisplay() { var isChecked = $checkbox.is(':checked'); // Set the button's state $button.data('state', (isChecked) ? "on" : "off"); // Set the button's icon $button.find('.state-icon') .removeClass() .addClass('state-icon ' + settings[$button.data('state')].icon); // Update the button's color if (isChecked) { $button .removeClass('btn-default') .addClass('btn-' + color + ' active'); } else { $button .removeClass('btn-' + color + ' active') .addClass('btn-default'); } } // Initialization function init() { updateDisplay(); // Inject the icon if applicable if ($button.find('.state-icon').length == 0) { $button.prepend('<i class="state-icon ' + settings[$button.data('state')].icon + '"></i> '); } } init(); }); });

Related: See More


Questions / Comments:

This model will no longer work in new browsers as exactly above. If the input field is styled display:none; browsers will not submit the field value.
Instead the inputs must be styled Visible:hidden; position:absolute;
We used this on our site and IE began omitting the hidden checkbox fields in September 2014, now the latest release of FF/C/S are doing the same.

Peter Postma () - 8 years ago - Reply 0


Really nice! How can I make this work with Radio, please help!

July Zerg () - 8 years ago - Reply 0


i want select only one checkbox in a group, How could it?. Thanks

Juan José Romero () - 9 years ago - Reply 0


I have a problem. After Javascript append new checkbox. The new button won't change state after clicked. But all the style look fine.

Guest () - 10 years ago - Reply 0


very cool ;)

Marlon () - 10 years ago - Reply 0


nice

guest () - 10 years ago - Reply 0


Thanks!

Travis Layne () - 10 years ago - Reply 0


I have recreated my situation: http://jsfiddle.net/ivawzh/...

Guest () - 10 years ago - Reply 0


Hi Travis, would you please make an example show me how to append new checkbox with your functionality? My new appended checkbox are broken.

Guest () - 10 years ago - Reply 0


It's not working because you're using multiple ID's with the same value.

Alex () - 10 years ago - Reply 0


Hello Travis and Alex, finally what was the solution ? (because its the same for me), thanks in advance

marcmarin () - 9 years ago - Reply 0