Posted By

danhanly on 01/20/11


Tagged

form javascript html array checkboxes


Versions (?)

Processing Checkbox Group


 / Published in: HTML
 

  1. function checkBoxes(){
  2. var result, vals = [], checkBoxName = document.forms['formName']['checkBoxName[]'];
  3. for(var i=0,elm;elm = checkBoxName[i];i++) {
  4. if(elm.checked) {
  5. vals.push(encodeURIComponent(elm.value));
  6. }
  7. }
  8. result = vals.join(', ');
  9. }
  10. <form name="formName">
  11. <input type="checkbox" name="checkBoxName[]" value="Value 1" />
  12. <input type="checkbox" name="checkBoxName[]" value="Value 2" />
  13. <input type="checkbox" name="checkBoxName[]" value="Value 3" />
  14. <input type="checkbox" name="checkBoxName[]" value="Value 4" />
  15. <input type="checkbox" name="checkBoxName[]" value="Value 5" />
  16. <input type="button" onClick="checkBoxes()" />
  17. </form>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: danhanly on January 20, 2011

This snippet processes an array of checkboxes and joins them all as below:

Value 1, Value 4, Value 5

You need to login to post a comment.