Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox unckecked value 1

Status
Not open for further replies.

rmagan

Programmer
Jun 3, 2003
35
US
How can I set a value for a checkbox that is not checked?
When it's checked it sends 'on'. I need it to send something (ie. 'off' ) when it's unchecked.

<input type=&quot;checkbox&quot; name=&quot;p_drop_class&quot;>
 
Code:
<script language=&quot;Javascript&quot;>
function checkFunction(obj) {
   if (obj.checked) {
      obj.value = &quot;on&quot;;
   }
   else {
      obj.value = &quot;off&quot;;
   }
}
</script>

<input type=&quot;checkbox&quot; name=&quot;p_drop_class&quot; onclick=&quot;checkFunction(this)&quot; value=&quot;off&quot;>
When it's checked it sends 'on'

Not exactly sure what you mean by &quot;send&quot; but if you need to pass a variable somewhere just pull it from formname.p_drop_class.value


-kaht

banghead.gif
 
I tried this and it didn't work for me. It seems that no matter what you set the value at, if the box is not checked, the reference to that value (at least in ASP) is always blank. It only provides the value if the box is checked when the form was submitted. The only way I was able to make it work was to loop through the form elements just before posting, change the values based on checked or unchecked, then check all of the boxes just before posting the form, but after changing the values. That was the only way I could make it send a value for a checkbox that was initially unchecked. It looked sort of funny when posting the form because all of the checkboxes would suddenly become checked, but I used some dynamic HTML to resolve that. Anyone have any thoughts on this ?

ToddWW
 
I think that checkboxes only send their values if they are checked...

You could have a hidden element associated with each checkbox to hold if it is on or off, but that seems like excessive coding...

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top