Forum Moderators: open

Message Too Old, No Replies

vars MIA

         

murphy57

3:56 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



Hi All,
I have 24 checkboxes and I need to grab the checked values and append them to the text field.

So I have this code and when I check a box the alert() fires then in the text box I get undefined. If I click all three I get undefined,undefined,undefined, So where are my var's.

================================
function getchecked() {
alert("SSS");
var newtxt = '';
var chkbx = document.getElementsByTagName('input');
for(var i = 0; i < chkbx.length; i ++) {
if(chkbx[i].type == 'checkbox' && chkbx[i].checked === true) {
if(newtxt.length !== 0) {
newtxt += ',';
}
newtxt += chkbx.innerHTML;
}
}
document.formName.marktext.value = newtxt;
}
</script>

<body>

<form name="formName">
<input name="marktext" type="text" value="" size="120">
<br>

<input type="checkbox" name="input" value = "WORD" onclick="getchecked()">Word<br>

<input type="checkbox" name="input" onclick="getchecked(this,'Type')">Type<br>

<input type="checkbox" name="input" onclick="getchecked(this,'Other')" value="S">Other<br>
</form>
==============================

Thanks
Bobby

Fotiman

12:39 am on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! I believe this line:

newtxt += chkbx.innerHTML;

should read:

newtxt += chkbx[i].innerHTML;

[edited by: Fotiman at 12:39 am (utc) on Nov. 20, 2008]

murphy57

2:25 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Hi Fotiman
Thanks for your reply. It got me a bit further, however I still can not get the var's to populate the text field.

Can I ask for some more assistance?

Thanks
Bobby

astupidname

2:25 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



When dealing with inputs don't work with innerHTML, use value instead. You are forgetting to add chkbx[i].value to the newtxt variable, and your innermost 'if' is not necessary as that can be checked with the other checks in your outer 'if' statement. Try it like this:
function getchecked() {
alert("SSS");
var newtxt = '';
var chkbx = document.getElementsByTagName('input');
for(var i = 0; i < chkbx.length; i ++) {
if(chkbx[i].type == 'checkbox' && chkbx[i].checked === true && chkbx[i].value.length > 0) {
newtxt += chkbx[i].value+', ';
}
}
document.formName.marktext.value = newtxt;
}

astupidname

2:33 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



Although, actually I don't see why you are looping through all the inputs each time an input is checked. Could just add that individual input's value to the marktext inputs value each time another checkbox is checked on, rather than looping through all of them each time one gets checked.

murphy57

3:24 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



astupidname
That's it!

Thanks for your time and knowledge
Bobby