Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe to an MVC checkbox change event in JQUERY

I'm trying to latch onto a checkbox change event using jQuery, currently I have this:

<script type="text/javascript">
    $(document).ready(function () {
        $('.timepicker').datetimepicker({ datepicker: false, format: 'H:i' });
        $('.mondaystartfinish').hide();

        //subscribe to change events
        $('#IsMonday').change(function () {
            RunsOnMondays();
        });
    });

    function RunsOnMondays() {
        if ($('#IsMonday').prop('checked') == 'true') {
            $('.mondaystartfinish').slideDown();
        } else {
            $('.mondaystartfinish').slideUp();
        }
    }    
</script>

However in debug when I check/uncheck the box the RunsOnMondays() method is not firing.

I have tried using this also but still didn't work:

$('IsMonday').prop('checked').change(function () { RunsOnMondays();});

Browser output

<div class="form-group">
    <label class="control-label col-md-2" for="IsMonday">Monday</label>
    <div class="col-md-10">
        <input class="check-box" data-val="true" data-val-required="The IsMonday field is required." id="IsMonday" name="IsMonday" type="checkbox" value="true" /><input name="IsMonday" type="hidden" value="false" />
        <span class="field-validation-valid" data-valmsg-for="IsMonday" data-valmsg-replace="true"></span>
    </div>
</div>

<div class="startfinish">
    <div class="form-group">
        <div class="mondaystartfinish">
            <label class="control-label col-md-2" for="MondayFrom">Monday Start</label>
            <div class="col-md-10">
                <input class="timepicker" data-val="true" data-val-date="The field MondayFrom must be a date." id="MondayFrom" name="MondayFrom" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="MondayFrom" data-valmsg-replace="true"></span>
            </div>
        </div>
    </div>
</div>

<div class="startfinish">
    <div class="form-group">
        <div class="mondaystartfinish">
            <label class="control-label col-md-2" for="MondayTo">Monday Finish</label>
            <div class="col-md-10">
                <input class="timepicker" data-val="true" data-val-date="The field MondayTo must be a date." id="MondayTo" name="MondayTo" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="MondayTo" data-valmsg-replace="true"></span>
            </div>
        </div>
    </div>
</div>
like image 351
JsonStatham Avatar asked Feb 02 '15 15:02

JsonStatham


1 Answers

The reason it does not work is that this is not true true == 'true' is actually false: http://jsfiddle.net/TrueBlueAussie/2wwz1ore/6/

prop('checked') returns a boolean value! No need for == 'true'.

http://jsfiddle.net/TrueBlueAussie/2wwz1ore/1/

$(document).ready(function () {
    //$('.timepicker').datetimepicker({ datepicker: false, format: 'H:i' });
    $('.mondaystartfinish').hide();

    // subscribe to change events
    $('#IsMonday').change(function () {
        RunsOnMondays();
    });
});

function RunsOnMondays() {
    if ($('#IsMonday').prop('checked')) {
        $('.mondaystartfinish').slideDown();
    } else {
        $('.mondaystartfinish').slideUp();
    }
}

Better yet, use slideToggle(): http://jsfiddle.net/TrueBlueAussie/2wwz1ore/3/

    // subscribe to change events
    $('#IsMonday').change(function () {
        $('.mondaystartfinish').slideToggle($(this).prop('checked'));
    });

If (and I very much doubt it) your element is added dynamically, use a delegated event handler attached to a non-change ancestor element (document is the default if nothing else is closer/convenient)

e.g.

    $(document).on('click' , '#IsMonday', function () {
        $('.mondaystartfinish').slideToggle($(this).prop('checked'));
    });
like image 69
Gone Coding Avatar answered Oct 02 '22 11:10

Gone Coding