...

View Full Version : Help needed with checkbox/dropdown code.



Digganob
06-15-2010, 05:35 AM
Hello everyone. I have been trying 2 days straight to figure out how to make this script work. Basically I am trying to have a hidden checkbox with a drop down menu. I want the user to be able to choose 0-9 where 0 the check box will not be checked and 1-9 if picked from the dropdown menu it will automatically check the hidden box. Here is the closest string of code I could find out there to help me figure it out but I am having no luck. The closest I got was....well I never really figured out how to make it work. Can someone please help me? Thanks.

<script type="text/javascript">
function checkUncheck (selected) {
var checkBox = document.getElementById("box");

if ((selected == 2) || (selected == 3)) {
checkBox.checked = "checked";
}
if ((selected == 0) || (selected == 1)) {
checkBox.checked = "false";
}
}
</script>
<form id="frm">
<table>
<tr>
<td><input type="checkbox" id="box" checked="false"/></td>

<td>
<select id="sel0" onchange="checkUncheck(this.selectedIndex)">
<option value="zero">zero</option>
<option value="one">one</option>

<option value="two">two</option>
<option value="three">three</option>
</select>
</td>
</tr>

</table>
</form>

Old Pedant
06-15-2010, 05:45 AM
<form ...>
...
<select name="whatever" onchange="this.form.theBox.checked = ( this.value != "0" );">
<option value="0">zero</option>
<option value="8">apples</option>
<option value="3">bananas</option>
...
</select>
<input type="checkbox" name="theBox" style="visibility: hidden;">
...
</form>

Old Pedant
06-15-2010, 05:47 AM
Why do you give id's to form fields? If you will submit the <form> to another page (e.g., PHP or ASP or JSP or whatever) fields without names are *NEVER* submitted. The id's are useless. (Excepting that if you want to use <label for="xxx"> then the "xxx" must be an id, not a name.)

Old Pedant
06-15-2010, 05:49 AM
If you don't want the value= of the <option>s to be numbers, then you could indeed use selectedIndex, thus:


<form ...>
...
<select name="whatever" onchange="this.form.theBox.checked = ( this.selectedIndex != 0 );">
<option value="frammis">zero</option>
<option value="imacs">apples</option>
<option value="yellow">bananas</option>
...
</select>
<input type="checkbox" name="theBox" style="visibility: hidden;">
...
</form>

Old Pedant
06-15-2010, 05:50 AM
Or, come to think of it, you could even do this:


<form ...>
...
<select name="whatever" onchange="this.form.theBox.checked = ( this.value != 'frammis' );">
<option value="frammis">zero</option>
<option value="imacs">apples</option>
<option value="yellow">bananas</option>
...
</select>
<input type="checkbox" name="theBox" style="visibility: hidden;">
...
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum