/    Sign up×
Community /Pin to ProfileBookmark

Help changing hiddden field

Hi,

I am writing a script which contains several check boxes, one of which selects all of the other check boxes. When check boxes are ticked the information from their values is put into a hidden field as a list and if it is un-ticked it is removed. When the one which selects all of the other check boxes is ticked I want all there values to be added to the list.

I cannot get the script to fill in the hidden field when the all check box is selected.

The code is,

[CODE]
<html>
<head>
<title>untitled</title>
<script type=”text/javascript” language=”javascript”>

function display(box) {
// get reference to form object, and to array of same-named checkboxes
var temparr = new Array(), f = box.form, boxgroup = f[box.name];
// loop through it
for (var i=0; i<boxgroup.length; i++) {
// add the value of any checked box to next available slot in temparr
if (boxgroup[i].checked) temparr[temparr.length] = boxgroup[i].value;
// run the .join() method on the array (separator = ‘,’) and output it to field
f.readout.value = temparr.join(‘,’);
}
}

function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

</script>
</head>
<body onLoad=”document.forms[0].reset()”>

<form name=”myform”>

<ul>
<li><label><input type=”checkbox” name=”Check_ctr” value=”yes” onClick=”Check(document.myform.group)”/></label> All Africa</li>

<li><label><input type=”checkbox” name=”group” value=”01A” onclick=”display(this)” onChange=”display(this)”/></label> Algeria</li><li><label><input type=”checkbox” name=”group” value=”02A” onclick=”display(this)” onChange=”display(this)”/></label> Angola</li><li><label><input type=”checkbox” name=”group” value=”03A” onclick=”display(this)” onChange=”display(this)”/></label> Benin</li><li><label><input type=”checkbox” name=”group” value=”04A” onclick=”display(this)” onChange=”display(this)”/></label> Botswana</li><li><label><input type=”checkbox” name=”group” value=”05A” onclick=”display(this)” onChange=”display(this)”/></label> Burkina_Faso</li><li><label><input type=”checkbox” name=”group” value=”06A” onclick=”display(this)” onChange=”display(this)”/></label> Burundi</li><li><label><input type=”checkbox” name=”group” value=”07A” onclick=”display(this)” onChange=”display(this)”/></label> Cameroon</li><li><label><input type=”checkbox” name=”group” value=”08A” onclick=”display(this)” onChange=”display(this)”/></label> Canary_Islands</li><li><label><input type=”checkbox” name=”group” value=”09A” onclick=”display(this)” onChange=”display(this)”/></label> Cape_Verde</li><li><label><input type=”checkbox” name=”group” value=”10A” onclick=”display(this)” onChange=”display(this)”/></label> Central_African_Republic</li><li><label><input type=”checkbox” name=”group” value=”11A” onclick=”display(this)” onChange=”display(this)”/></label> Chad</li><li><label><input type=”checkbox” name=”group” value=”12A” onclick=”display(this)” onChange=”display(this)”/></label> Comoros</li><li><label><input type=”checkbox” name=”group” value=”13A” onclick=”display(this)” onChange=”display(this)”/></label> Congo</li><li><label><input type=”checkbox” name=”group” value=”14A” onclick=”display(this)” onChange=”display(this)”/></label> Democratic_Republic_of_Congo</li><li><label><input type=”checkbox” name=”group” value=”15A” onclick=”display(this)” onChange=”display(this)”/></label> Djibouti</li>

</ul>
<br><input type=”hidden” name=”readout” size=”20″>

</form>
</body>
</html>
[/CODE]

Can anyone help solve this?

Thanks,
Cs1h

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinJan 28.2010 — [code=html]<html>
<head>
<title>untitled</title>
<script type="text/javascript">

function tally(){ var temp=[]; var f0e=document.forms[0].elements;
if(f0e[0].checked){for(i=0;i<f0e.length-1;i++){f0e[i].checked=true}}
for(i=0;i<f0e.length-1;i++){if(f0e[i].checked==true){temp.push(f0e[i].value)}}
f0e[f0e.length-1].value=temp.join(',')
}
</script>
</head>
<body onLoad="document.forms[0].reset()">


<form name="myform">
<script type="text/javascript">
var vaf='yes_All Africa,01A_Algeria,02A_Angola,03A_Benin,04A_Botswana,05A_Burkina_Faso,06A_Burundi,07A_Cameroon,08A_Canary_Islands,09A_Cape_Verde,10A_Central_African_Republic,11A_Chad,12A_Comoros,13A_Congo,14A_Democratic_Republic_of_Congo,15A_Djibouti';
vaf=vaf.split(',')
var t='<ul>';
for(i=0;i<vaf.length;i++){ var v=vaf[i].substring(0,vaf[i].indexOf('_'))
var c=vaf[i].substring(vaf[i].indexOf('_')+1,vaf.length)
t+='<li><LABEL><input type=checkbox value="'+v+'" onclick="tally()"> '+c+' </LABEL></li>';
}
document.write(t)
</script>

</ul>
<br><input name="readout" id="readout" size="20">

</form>
</body>
</html>[/code]
×

Success!

Help @cs1h spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...