I have this group of checkboxes displayed using a while loop form sql select. The checkboxes have values assigned to them from sql. I have assigned all the checkboxes the same class.
If I click on one of them, I want the value shown in an alert using jQuery`.
Code:
<?php
//$class=trim("YEAR 1");
$rec = mysqli_query($db, "SELECT * from studentbyclass where CLASS='$class' ");
while($row = mysqli_fetch_array($rec)){
?>
<label class="form-check form-switch">
<input class="form-check-input students" type="checkbox" value="<?php $row["STUDENT-ID"];?>" checked>
<span class="form-check-label students"><?php echo $row["STUDENT-NAME"];?></span>
</label>
<?php
}
?>
JQUERY
$(".students").on('click' , function(){
val = this.val();
alert(val);
});
thisis a html element. Wrap it in a jquery object to use .val() on it. E.g.$(this).val(). Or skip the overhead of making a jquery object for something so trivial and just writethis.value. developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/value