changing a hidden field value from a checkbox

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 17
Joined: Sat Jun 18, 2005 9:08 pm

Wed Jul 05, 2006 1:11 pm

I have a checkbox and hidden field within a form :

Code: Select all

<input name="checkbox1" id="checkbox1" type="checkbox" value="abc" onClick="document.forms[0].hidden1.value = '1'>

<input name="hidden1" id="hidden1" type="hidden" value="0">
So, basically I'm changing the value of the hidden form from the checkbox onClick...Which is fine...But I also want when you click the checkbox again (to untick it) the hidden field reverts back to it's old value.

So I want if you click the checkbox first to tick it, the hidden1 value is put to 1. Then if you click that same checkbox again to untick it, I want the hidden1 value changed back to 0.

How do I do it?


User avatar
Posts: 826
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Fri Jul 07, 2006 3:30 pm

make a function that checks the value.

Code: Select all

<script type="text/javascript">
if(document.getElementById(hidden).value == 1){
  document.getElementById(hidden).value =2;
} else {
  document.getElementById(hidden).value = 1;


<input name="checkbox1" id="checkbox1" type="checkbox" value="abc" onClick="checkHidden('hidden1');>
<input name="hidden1" id="hidden1" type="hidden" value="0">

somthing like that.

Post Reply