How does one uncheck a check Box with Javacript?

Hello,

How does one uncheck a check Box with Javacript when the rows of the Table are dynamically created by
Php so we do not know what the row numbers are going to be before hand.

That is we have these 2 check boxes:

<input type=“checkbox” name=“approve” value=“<?php echo $user_id; ?>” checked onClick=“uncheck(reject, <?php echo ($row - 1); ?>>
&
<input type=“checkbox” name=“reject” value=”<?php echo $user_id; ?>" onClick=“uncheck(approve, <?php echo ($row - 1); ?>”>

So if they click on check box “reject”, we then want check box “approve” unchecked and vis-versa.

FYI, I was using this Javascript code:

function uncheck(field, i)
{
field[i].checked = false ;
}

But the combination of above is not working!

ThanX,

Sounds like you should be using radio buttons instead of check boxes.

Well the reason I need to use Checkboxes is that I need the values selected in all the various rows added to an array, which then I can loop through, that is what check box as an array allows but radio which holds a single value does not.

Or are you saying that there is a form of a radio which allows for collecting the selected values in an array, that is:

In check box we have:

<input type=“checkbox” name=“reject” value=“<?php echo $user_id; ?>”>

is the same possible in a radio like:

<input type=“radio” name=“reject” value=“<?php echo $user_id; ?>”>
whereby then reject would be an array of values filled by radio?

So if they click on check box “reject”, we then want check box “approve” unchecked and vis-versa.

Then why do you want to uncheck another check box when one is clicked if you want all the selected values?

You’re posting only bits and pieces of your code and it’s not consistent with what you say you want to do.

Hopefully someone else will come along to show you what you have done incorrectly.

Ok, no problem. Thanks anyway.

Try

checked = null;

Looks like you’re missing the ) after the parameter. Use the error console.

Does this do what you need?

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">

function uncheck( group )
{
  if( group.length )
  {
   for( var n = 0; group[ n ]; n++ )
    group[ n ].checked = false;
  }
  else
   group.checked = false ;
}

</script>

</head>
<body>
<p>
<form action='#'>
 <input type="checkbox" name="approve[]" value="XXX" checked onClick="uncheck(this.form[ 'reject[]' ] )">

 <input type="checkbox" name="reject[]" value="YYY" onClick="uncheck(this.form[ 'approve[]' ] )">
 <input type="checkbox" name="reject[]" value="ZZZ" onClick="uncheck(this.form[ 'approve[]' ] )">
</form>
</body>
</html>