Jump to content

[SOLVED] Grabbing multiple checkbox


Recommended Posts

I have a form, in the form, there is this:

<td>Age Group</td>
<td>
<input type="checkbox" name="age[]" value="24" /> 2-4<br />
<input type="checkbox" name="age[]" value="57" /> 5-7<br />
<input type="checkbox" name="age[]" value="812" /> 8-12
</td>

 

How can I (using javascript) get which ones are selected? I'll be sending it to a PHP script using AJAX

 

Wes

Link to comment
https://forums.phpfreaks.com/topic/107675-solved-grabbing-multiple-checkbox/
Share on other sites

You can access it with:

document.forms[0]['age[]']

which will give you a node list. So, loop over the list, test if it's checked, and do what you wish if it is:

var eles = document.forms[0]['age[]'];
for(var i = 0;i < eles.length;i++){
  if(eles[i].checked)
    alert(eles[i].value);
}

 

note: can someone be in more then one age group? if not, these should be radio buttons

gotchya, checkboxes it is....

 

more notes: if you are trying to build the data by looping over the elements in your form before sending it with ajax, might i recommend checking out a JS library like jQuery which allows you to attach a form node to an AJAX call. might save you a bunch of coding.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.