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 Link to comment https://forums.phpfreaks.com/topic/107675-solved-grabbing-multiple-checkbox/ 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 Link to comment https://forums.phpfreaks.com/topic/107675-solved-grabbing-multiple-checkbox/#findComment-551947 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 Link to comment https://forums.phpfreaks.com/topic/107675-solved-grabbing-multiple-checkbox/#findComment-551957 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. Link to comment https://forums.phpfreaks.com/topic/107675-solved-grabbing-multiple-checkbox/#findComment-551960 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 Link to comment https://forums.phpfreaks.com/topic/107675-solved-grabbing-multiple-checkbox/#findComment-552040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.