javascript - Filling in a textbox with values from checkboxes -
here code have far
<head> <script type="text/javascript"> function list(c,n,z) { s=document.form.marktext.value; if (c.checked) { if (s.indexof(n)<0) s+=', '+n; } else { s=document.form.marktext.value.replace(', '+n,''); } z=", "; if (s.substring(2) == z) s=s.substring(2); document.form.marktext.value=s;} function getchecked() { var newtxt = ''; var chkbx = document.getelementsbytagname('input'); for(var = 0; < chkbx.length; ++) { if(chkbx[i].type == 'checkbox' && chkbx[i].checked === true) { if(newtxt.length !== 0) { newtxt += ','; } newtxt += chkbx.innerhtml; } } document.form.marktext.value = newtxt; } </script> </head> <body> <form name="form"> <input type="text" value="" name="marktext"><br> <input type="checkbox" name="mark" value="word" onclick="getchecked()">word<br> <input type="checkbox" name="mark" onclick="getchecked()">type<br> <input type="checkbox" name="mark" onclick="getchecked()">other<br> </form> </body>
it works when click checkbox, getting 'undefined' label in textbox instead of checkbox value. guessing small , stupid can't seem figure out.
notice index on newtxt += chkbx[i].value;
instead of newtxt += chkbx.innerhtml;
<head> <script type="text/javascript"> function list(c,n,z) { s=document.form.marktext.value; if (c.checked) { if (s.indexof(n)<0) s+=', '+n; } else { s=document.form.marktext.value.replace(', '+n,''); } z=", "; if (s.substring(2) == z) s=s.substring(2); document.form.marktext.value=s;} function getchecked() { var newtxt = ''; var chkbx = document.getelementsbytagname('input'); for(var = 0; < chkbx.length; ++) { if(chkbx[i].type == 'checkbox' && chkbx[i].checked === true) { if(newtxt.length !== 0) { newtxt += ','; } newtxt += chkbx[i].value; } } document.form.marktext.value = newtxt; } </script> </head> <body> <form name="form"> <input type="text" value="" name="marktext"><br> <input type="checkbox" name="mark" value="word" onclick="getchecked()">word<br> <input type="checkbox" name="mark" onclick="getchecked()" value="type">type<br> <input type="checkbox" name="mark" onclick="getchecked()" value="other">other<br> </form> </body>
but suggestion use jquery or other javascript library!!
Comments
Post a Comment