0

Is there a reason why I don't see an alert when the checkbox is checked in my code?

jQuery(function($) {
  if ($('input#choice_3_37_1').is(':checked')) {
    alert('test');
    $("#label_3_37_1").addClass("gquiz-correct-choice");
  }
});
.gquiz-correct-choice {
  background: green;
  color: white
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="choice_3_37_1" name="change">

<hr>

<label id="label_3_37_1">this should change to green!</label>

3
  • 4
    When that code runs (after the DOM finishes rendering), the checkbox is not checked. You need an event handler for when it does change. Commented Jan 14, 2021 at 11:33
  • 1
    Yes, you dont have event to show you an alert... Put checked="checked" in your html and it will work. Commented Jan 14, 2021 at 11:34
  • 1
    Rookie error here. Thanks very much for highlighting. Commented Jan 14, 2021 at 11:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.