5

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>
5
  • note: prop('checked') returns a boolean value! No need for == 'true' Commented Feb 2, 2015 at 15:13
  • Please show your page HTML (preferably browser output and not source as that makes a JSFiddle easier to produce) :) Commented Feb 2, 2015 at 15:14
  • is your html being rendered dynamically(ajax) ? Commented Feb 2, 2015 at 15:17
  • 1
    Just removing == 'true' is enough for your code to work, but I recommend using slideToggle with a boolean value to cleanup the code See working examples below. Commented Feb 2, 2015 at 15:24
  • What does the HTML look like? Do you buy any chance have two elements with id of IsMonday? Commented Feb 2, 2015 at 15:25

2 Answers 2

6

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'));
    });
Sign up to request clarification or add additional context in comments.

9 Comments

But this can't be the fix for the event not firing
@Krishna: it is the fix... try the original code in the same JSFiddle and it will not work :)
As per OP, the RunsOnMondays() method is not firing
@SelectDistinct: just remember that true != 'true' :)
@Krishna: I added a delegated event option, but I don't assume zebras when I hear hooves :P
|
2

You could use the below as follows:

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


function RunsOnMondays(isChecked) {
    if (isChecked) {
        $('.mondaystartfinish').slideDown();
    } else {
        $('.mondaystartfinish').slideUp();
    }
}

jsFiddle

4 Comments

Not that this is wrong, but why change to click? $('#IsMonday').click(RunsOnMondays); might be a bit shorter too if the anonymous function's only job is to pass on the checked parameter.
@RGraham Yes agreed, I had changed it before you answered.
I should pay more attention :)
@RGraham I should when answering. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.