469,649 Members | 1,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

Setting a value to a hidden field

Jim
I have a 2 checkboxes and a hidden field..what I want to happen is
that you can only click on 1 of these checkboxes at a time and when
you check a checkbox it will assign the hidden field "txtreferral" a
value and when you uncheck the checkbox it will set the hidden field
value back to nothing ..I have the clicking on 1 check box at a time
down but when I try to add the code to assign the value to the hidden
field I get an error in the javascript console..heres my code so far:

The two checkboxes are named txtreferral1 and txtreferral2 here is the
code for txtreferral2....

onclick="if(txtreferral1.checked)txtreferral1.chec ked=!this.checked;txtreferral.value="EMPS""

thanks

-Jim
Jul 23 '05 #1
1 5350
Lee
Jim said:

I have a 2 checkboxes and a hidden field..what I want to happen is
that you can only click on 1 of these checkboxes at a time and when
you check a checkbox it will assign the hidden field "txtreferral" a
value and when you uncheck the checkbox it will set the hidden field
value back to nothing ..I have the clicking on 1 check box at a time
down but when I try to add the code to assign the value to the hidden
field I get an error in the javascript console..heres my code so far:

The two checkboxes are named txtreferral1 and txtreferral2 here is the
code for txtreferral2....

onclick="if(txtreferral1.checked)txtreferral1.che cked=!this.checked;txtreferral.value="EMPS""


1. What did the error message say?

2. Why use Javascript at all? Why not:
<input type="radio" name="txtreferral" value=""> None<br>
<input type="radio" name="txtreferral" value="OTHER"> Other<br>
<input type="radio" name="txtreferral" value="EMPS"> Emps<br>

3. Here's a very non-general solution:

Note that we don't care about the current state of the
other box or of the hidden value when a box is clicked.

<html>
<head>
<script type="text/javascript">
function boxclick(box,value){
var hidden=box.form.elements["txtreferral"];
if(box.checked){
var otherBoxName="txtreferral"+(3-box.name.charAt(box.name.length-1));
box.form.elements[otherBoxName].checked=false;
hidden.value=value;
}else{
hidden.value="";
}
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="txtreferral1" onclick="boxclick(this,'OTHER')">
Other<br>
<input type="checkbox" name="txtreferral2" onclick="boxclick(this,'EMPS')">
Emps<br>
<input name="txtreferral" value="">
</form>
</body>
</html>

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Groom | last post: by
4 posts views Thread by PJ6 | last post: by
8 posts views Thread by Efi Merdler | last post: by
5 posts views Thread by Dave Rado | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.