Daniele Martini
Daniele Martini

Reputation: 143

Check checkbox status jQuery

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?

Upvotes: 0

Views: 526

Answers (1)

David Thomas
David Thomas

Reputation: 253338

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");
        }
    });
});

Upvotes: 1

Related Questions