Search code examples
jquerytwitter-bootstrapbootstrap-switch

Bootstrap Switch check state doesn't work


So I'm working on my usergroups page on my cms, and I added a switch to determine if the group has access to the admin section or not. If so (and the user checks the switch), a div of permissions will be displayed. For this, I used Bootstrap's plugin Bootstrap Switch.

Here is the code:

$(document).ready(function() {
            $('input#isAdmin').bootstrapSwitch();
            $('#permissions_multi_select').multiSelect();
            $('#permissions_div').hide();

            $('input#isAdmin').on('switchChange.bootstrapSwitch', function() {
                if ($('input#isAdmin').state)
                    $('#permissions_div').show();
                else
                    $('#permissions_div').hide();
            });
        });

If I change the show() or hide() methods with an alert of $('input#isAdmin').state, or .attr('checked') or .checked, I get 'undefined'.

Any ideas?


Solution

  • Replace $('input#isAdmin').state with $('input#isAdmin').bootstrapSwitch('state') in the condition

    $('input#isAdmin').bootstrapSwitch();
    $('#permissions_multi_select').multiSelect();
    $('#permissions_div').hide();
    
    $('input#isAdmin').on('switchChange.bootstrapSwitch', function() {
        if ($('input#isAdmin').bootstrapSwitch('state'))
            $('#permissions_div').show();
        else
            $('#permissions_div').hide();
    });
    

    Working fiddle: http://jsfiddle.net/o2y5mhpr/1/