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?

3
  • 1
    Possible dupplication stackoverflow.com/questions/2834350/… Commented Mar 18, 2016 at 13:54
  • Have you tried manually setting "checked" attribute and see if that is being logged? <input type="checkbox" id="checkOrarioContinuato" value="1" checked> Commented Mar 18, 2016 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. Commented Mar 18, 2016 at 14:01

1 Answer 1

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

Comments

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.