Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab checkbox values before form submission

Tags:

jquery

php

I have the following loop, which shows a checkbox along with an answer (which is grabbed from Wordpress):

$counter = 1; 
foreach ($rows as $row){ ?>
   <input type="checkbox" name="answer<?php echo $counter; ?>[]" value="<?php echo the_sub_field('answer'); ?>" />
   <?php echo $row['answer'];
} ?>

This is part of a bigger loop that loops through a set of questions and for each question it loops through the answers (code above).

How can I grab the checkboxes that the user has checked and display the values within a div before the form is submitted?

I know I can use the following to check if the checkbox is checked:

$('form #mycheckbox').is(':checked');

I'm not sure where to start with all the looping!

like image 552
Rob Avatar asked Jan 31 '26 22:01

Rob


1 Answers

You can use the selector :checked

$.each("#mycheckbox:checked", function() {
    $("div").append(this.val());
});
like image 77
97ldave Avatar answered Feb 03 '26 14:02

97ldave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!