...

View Full Version : Show/hide array of textboxes and checkboxes?



workinprogress
08-14-2008, 01:30 AM
Hi all,

Major noob here; what I want to do is show/hide an array consisting of one checkbox (ID=pss) and two textbox fields (IDs=images and bases) when a checkbox is checked/unchecked (show when checked) I do not know how to toggle the array off and on, and I've been searching the web for four hours now...about to pull my hair out, so any help at all is MUCH appreciated...here's the lame code I have so far:

JS:
<script type="text/javascript">
function Toggle1
var arrayJP = new Array("pss","images","bases"); {


</script>

HTML:
<span class="formw">
<span class="span-1">
<input type="checkbox" name="Language 1:" id="lang1" value="Japanese" onclick="Toggle1('arrayJP', this)"></span>
<span class="span-2">Japanese</span>

ninnypants
08-14-2008, 01:45 AM
I believe something like this would work


function toggle(val){
if(val){
for(i=0;i<arrayJP.length;i++){
document.getElementById(arrayJP[i]).style.display = 'block';
}
}else{
for(i=0;i<arrayJP.length;i++){
document.getElementById(arrayJP[i]).style.display = 'none';
}
}
}


<input type="checkbox" name="Language 1:" id="lang1" value="Japanese" onchange="toggle(this.value)">
All it does is checks to see if the checkbox has a value if it does they display, and if it doesn't then the check boxes will be hidden

workinprogress
08-14-2008, 03:18 AM
Thank you so much! I think that's getting me closer...

I have this, which I'm ~assuming~ identifies the IDs (one checkbox and two text boxes) pss, images, and bases as arrayJP. THEN, in the code for the checkbox I want to display the array when checked (id=info), I call the function. However, when I check the checkbox, nothing happens...it appears to me that the info.length values are identical for showing and hiding, should they be different? (The styles of the three array elements are set to display:none)

Javascript:
<script type="text/javascript">
function toggle(val){
var arrayJP = new Array("pss","images","kbases"); {
if(val){
for(i=0;i<info.length;i++){
document.getElementById(arrayJP[i]).style.display = 'block';
}
}else{
for(i=0;i<info.length;i++){
document.getElementById(arrayJP[i]).style.display = 'none';
}
}
}
</script>

HTML:
<span class="span-1">
<input type="checkbox" id="info" name="Required" value="" onchange="toggle(this.value)"></span>
<span class="span-2">Required</span>
</span>

ninnypants
08-14-2008, 04:07 AM
This needs to be the arrays name so that it goes through and hides all the elements.

for(i=0;i<info.length;i++)
Also if you aren't going to give your check box a value you'll need to do it "onclick" instead of "onchange"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum