Click to See Complete Forum and Search --> : checkbox value invert problems


gvimercati
08-29-2003, 05:54 AM
hello, im a newy to this forum... and to javascript for that matter, my situation is...:

i have 20 or checkboxes in a form, the form Posts to itself because it needs server side validation, i need to make sure that if the checkbox is ticked and the form is submitted, the value of the checkbox changes b4 the submission... the way i am doing this is with am onChange='Invert(this.name);'

function Invert(inputName)
{
//doco below doesn't output the number... only the string
doco = "document.form."+inputName+".value";

if (doco== 1)
doco= 0;
else
doco= 1;
}

im trying to reuse the code as much as possible or else i know that i could simply write 20 if statements... there has to be a reusable way... can someone shed some light...

in simpler terms, i need 'doco' to give the value not the string..

im open to all suggestions.. or if there is another way of doing this... i'm all ears

thanks for the patience...

Fang
08-29-2003, 07:02 AM
If you want to uncheck all checkboxes before submission do it like this:

function Invert(obj) {
for(var i=0; i<obj.length; i++) {
if(obj.elements[i].checked==true) {
obj.elements[i].checked=false;
}
}
obj.submit();
}
.
.
<form action="NextPage.html" method="post" name="myform" onsubmit="Invert(this); return false;">
<input type="checkbox" value="box1" />
<input type="checkbox" value="box2" />
<input type="checkbox" value="box3" />
<button type="submit">submit</button>
</form>