"jQuery Checkbox Buttons"
Bootstrap 4.0.0 Snippet by edsb

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/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>
.hidden { visibility: hidden; } /* Remove hover from primary outline buttons on mobile */ .no-hover.btn-primary:hover { color: #fff; background-color: #0275d8; border-color: #0275d8; } .no-hover.btn-secondary:hover { color: #292b2c; background-color: #fff; border-color: #ccc; }
$(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"); // Update the button's color if (isChecked) { $button .removeClass('btn-secondary') .addClass('btn-' + color); } else { $button .removeClass('btn-' + color) .addClass('btn-secondary'); } } // Initialization function init() { updateDisplay(); } init(); }); }); $(document).on('touchstart', function(){ // once a touch has been detected $("button").addClass("no-hover"); // add the no-hover class to all labels });

Related: See More


Questions / Comments: