Toggle navigation
Bootsnipp
Bootstrap
For
CSS Frameworks
Bootstrap
Foundation
Semantic UI
Materialize
Pure
Bulma
References
CSS Reference
Tools
Community
Page Builder
Form Builder
Button Builder
Icon Search
Dan's Tools
Diff / Merge
Color Picker
Keyword Tool
Web Fonts
.htaccess Generator
Favicon Generator
Site Speed Test
Snippets
Featured
Tags
By Bootstrap Version
4.1.1
4.0.0
3.3.0
3.2.0
3.1.0
3.0.3
3.0.1
3.0.0
2.3.2
Register
Login
"Fancy Radio/Checkbox Buttons"
Bootstrap 3.3.0 Snippet by
lookbadgers
3.3.0
jQuery
buttons
checkbox
radio
Preview
HTML
CSS
JS
View Full Screen
Fork
Fork this
7.9K
 
1 Fav
Post to Facebook
Tweet this
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css"> <!-- Latest compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-12"> <h2>Fancy Checkbox Buttons</h2> <div class="btn-group" role="group"> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-unchecked" data-icon-on="glyphicon glyphicon-check" data-icon-off="glyphicon glyphicon-unchecked"></span> <input name="checkbox" type="checkbox" value="ant" style="display: none" autocomplete="false"/> Ant </label> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-unchecked" data-icon-on="glyphicon glyphicon-check" data-icon-off="glyphicon glyphicon-unchecked"></span> <input name="radio" type="checkbox" value="bat" style="display: none" autocomplete="false"/> Bat </label> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-unchecked" data-icon-on="glyphicon glyphicon-check" data-icon-off="glyphicon glyphicon-unchecked"></span> <input name="checkbox[]" type="checkbox" value="cat" style="display: none" autocomplete="false"/> Cat </label> </div> </div> <div class="col-md-12"> <h2>Fancy Radio Buttons</h2> <div class="btn-group" role="group"> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-unchecked" data-icon-on="glyphicon glyphicon-check" data-icon-off="glyphicon glyphicon-unchecked"></span> <input name="radio" type="radio" value="ant" style="display: none" autocomplete="false"/> Ant </label> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-unchecked" data-icon-on="glyphicon glyphicon-check" data-icon-off="glyphicon glyphicon-unchecked"></span> <input name="radio" type="radio" value="bat" style="display: none" autocomplete="false"/> Bat </label> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-unchecked" data-icon-on="glyphicon glyphicon-check" data-icon-off="glyphicon glyphicon-unchecked"></span> <input name="radio" type="radio" value="cat" style="display: none" autocomplete="false"/> Cat </label> </div> </div> <div class="col-md-12"> <h2>Fancy Sort Order</h2> <div class="input-group"> <div class="form-control form-control-selectpicker"> <select class="selectpicker" name="sort" title="Sorting"> <option>Created</option> <option>Price</option> <option>Quantity</option> </select> </div> <div class="input-group-btn"> <label class="btn btn-default btn-checkbox"> <span class="glyphicon glyphicon-sort-by-attributes" data-icon-on="glyphicon glyphicon-sort-by-attributes-alt" data-icon-off="glyphicon glyphicon-sort-by-attributes"></span> <input name="order" type="radio" value="1" style="display: none" autocomplete="false"/> </label> </div> </div> </div> </div> </div>
.input-group .form-control.form-control-selectpicker { padding: 0; } .input-group .form-control.form-control-selectpicker > .btn-group.bootstrap-select { width: 100%; } .input-group .form-control.form-control-selectpicker .btn-group .btn { border: 0; }
$(function() { $('body').on('click','.btn-checkbox',function(e) { e.stopPropagation(); e.preventDefault(); var $checkbox = $(this).find(':input[type=checkbox]'); if ($checkbox.length) { var $icon = $(this).find('[data-icon-on]'); if ($checkbox.is(':checked')) { unchecked($checkbox); } else { checked($checkbox); } return; } var $radio = $(this).find(':input[type=radio]'); if ($radio.length) { var $group = $radio.closest('.btn-group'); $group.find(':input[type=radio]').not($radio).each(function(){ unchecked($(this)); }) var $icon = $(this).find('[data-icon-on]'); if ($radio.is(':checked')) { unchecked($radio); } else { checked($radio); } return; } }); }); function checked($input) { var $button = $input.closest('.btn'); var $icon = $button.find('[data-icon-on]'); $button.addClass('active'); $input.prop('checked', true); $icon.css('width',$icon.width()); $icon.removeAttr('class').addClass($icon.data('icon-on')); $input.trigger('change'); } function unchecked($input) { var $button = $input.closest('.btn'); var $icon = $button.find('[data-icon-on]'); $button.removeClass('active'); $input.prop('checked', false); $icon.css('width',$icon.width()); $icon.removeAttr('class').addClass($icon.data('icon-off')); $input.trigger('change'); }
Questions / Comments:
Post
Posting Guidelines
Formatting
- Now
×
Close
Donate
BTC: 12JxYMYi6Vt3mx3hcmP3B2oyFiCSF3FhYT
ETH: 0xCD715b2E3549c54A40e6ecAaFeB82138148a6c76