Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Switch, global checkbox to (un)check all other

I am using Bootstrap Switch for Twitter Bootstrap. Here I have problem to check or uncheck all checkbox based on global checkbox checked or unchecked

Here is the bootstrap switch link http://www.bootstrap-switch.org/

Here is what I have done

<!-- this is the global checkbox -->
<div class="make-switch switch-mini">
    <input type="checkbox" id="rowSelectAll">
</div>

<!-- row checkboxs -->
<div class="make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>

jQuery:

$("#rowSelectAll").click(function () {
    $(".row-select").prop('checked', $(this).prop('checked'));
});

EDIT: tried with only one global and one local checkbox

<div class="make-switch switch-mini">
    <input type="checkbox" id="rowSelectAll">
</div>

<div id="toggle-state-switch" class="make-switch switch-mini row-select">
    <input type="checkbox" class="">
</div>
like image 594
Code Lover Avatar asked Sep 20 '13 07:09

Code Lover


3 Answers

Try this one, it should work. Also see this jsfiddle...http://jsfiddle.net/URAcy/5/

  <!-- this is the global checkbox -->
<div class="make-switch switch-mini" id="rowSelectAll">
    <input type="checkbox" id="rowSelectAllc">
</div>

<!-- row checkboxs -->
<div class="make-switch switch-mini toggle-state-switch">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini toggle-state-switch">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini toggle-state-switch">
    <input type="checkbox" class="row-select">
</div>

JS:

$('#rowSelectAll').on('switch-change', function (e, data) {
    var $el = $(data.el)
      , value = data.value;
        $('.toggle-state-switch').each(function( index ) {
      $(this).bootstrapSwitch('setState' , value);
    });
});
like image 199
nik Avatar answered Nov 15 '22 20:11

nik


Try this:

 <!-- this is the global checkbox -->
<div class="make-switch switch-mini">
    <input type="checkbox" id="rowSelectAll">
</div>

<!-- row checkboxs -->
<div class="myswitch make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="myswitch make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="myswitch make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>

then in your script

    $('#rowSelectAll').on('change', function(){
    $('.myswitch').each(function(){

        if($(this).hasClass('switch-on')){
            $(this).bootstrapSwitch('setState' , false);
            $(this).removeClass('switch-on');
        }else{

       $(this).bootstrapSwitch('setState' , true);
        $(this).addClass('switch-on');
        }
    });
});
like image 28
Drixson Oseña Avatar answered Nov 15 '22 20:11

Drixson Oseña


updated your fiddle - [ http://jsfiddle.net/URAcy/6/ ]

It should be

$('#rowSelectAll').on('switch-change', function(){....}
like image 27
user2680900 Avatar answered Nov 15 '22 20:11

user2680900