0

I have some problem whit this custom checkbox that I made in this website. I'm trying to get the value status of the chekbox using jQuery and then hide a dive or sho if the checkbox is selected. This is the code:

<div class="row">
  <div class="col-md-12 form-inline">
    <div class="checkbox col-md-6">
      <input type="checkbox" id="checkOrarioContinuato" value="1">
      <label for="checkOrarioContinuato" class="cella paddingCella nomesalone" value="1">ORARIO CONTINUATO</label>
    </div>
    <div class="checkbox col-md-6">
      <input type="checkbox" id="checkOrarioNonContinuato" value="2">
      <label for="checkOrarioNonContinuato" class="cella paddingCella nomesalone">ORARIO NON CONTINUATO</label>
    </div>
  </div>
</div>

I tried to get the value of the checkbox using this script:

$(document).ready(function() {
    if ($('#checkOrarioContinuato').is(':checked')) {
        console.log("Orario Continuato selezionato");
    }

Where's my mistake?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Daniele Martini
  • 143
  • 1
  • 1
  • 13
  • 1
    Possible dupplication http://stackoverflow.com/questions/2834350/get-checkbox-value-in-jquery – HarrieDakpan Mar 18 '16 at 13:54
  • Have you tried manually setting "checked" attribute and see if that is being logged? `` – Morpheus Mar 18 '16 at 13:58
  • 1
    @Pranav: the linked question is not a duplicate; in that question the answer is how to retrieve the status/value of a checkbox. In this question the op clearly knows, and *shows* how to get the state/value. This question is, basically, about *when*, not *how*, to check that value. – David Thomas Mar 18 '16 at 14:01

1 Answers1

1

Your mistake is that you check the state of the check-box once, on document ready, not in response to the change event of the check-box.

Instead, try this approach, which checks the state and shows the <div> following the change event:

$(document).ready(function() {
    $('#checkOrarioContinuato').on('change', function () {
        if (this.checked) {
            console.log("Orario Continuato selezionato");
        }
    });
});
David Thomas
  • 249,100
  • 51
  • 377
  • 410