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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.