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>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190
  • 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. – Nick Jan 14 '21 at 11:33
  • 1
    Yes, you dont have event to show you an alert... Put checked="checked" in your html and it will work. – ikiK Jan 14 '21 at 11:34
  • 1
    Rookie error here. Thanks very much for highlighting. – michaelmcgurk Jan 14 '21 at 11:34

0 Answers0