therealwesfoster Posted May 28, 2008 Share Posted May 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 28, 2008 Share Posted May 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted May 28, 2008 Author Share Posted May 28, 2008 Awesome man thanks. And funny note but this is part of a search filter, sorting results by age group. Wes Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 28, 2008 Share Posted May 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted May 28, 2008 Author Share Posted May 28, 2008 I'll look into it thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.