var str = document.getElementsByName("box"); for (i = 0; i < str.length; i++) { if (str[i].checked == true) { sb = sb + str[i].value; if (i < str.length) { sb = sb + ","; } } } var endpos = sb.lastIndexOf(','); if (endpos > -1) { sb = sb.substring(0, endpos) + sb.substring(endpos + 1, sb.length); }