• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

Zero a checkbox object from a script

Hi,

I have a number of lines which uses some checkboxes in html like this:

<td>
<input type='checkbox' name='XXXcb' value='1' onclick='unCheckOthers(this);'>
<input type='checkbox' name='XXXcb' value='2' onclick='unCheckOthers(this);'>"
<input type='checkbox' name='XXXcb' value='3' onclick='unCheckOthers(this);'>"
</td>

and the unCheckOthers JS function looks like this:

function unCheckOthers(obj)
{
     var objGroup = obj.form[obj.name];
     var boxEmpty = true;
     
     for (var i = 0; i < objGroup.length; i++)
     {
          if (objGroup[i] != obj)
               objGroup[i].checked = false;

          if (objGroup[i].checked == true)
               boxEmpty = false;
     }

     if (boxEmpty == true)
          // checkbox has been unclicked, set value to zero
               obj.value = 0;
         
}

The first part of the js code unchecks the other two  checkboxes when one is clicked (making it somewhat in a radiobutton fashion, but with the option to zero all checkboxes), and that works fine. The old checking is removed when I click a new one.

Now, what I want is that whenever someone unclicks ALL of the checkboxes, the value of 'XXXcb' in the html shall be set to zero, from this javascript. It doesn't work when I set obj.value to 0, nor does it work if I loop through objGroup again to set all of those values to 0.

I MUST do this dynamically, as the X's in the name 'XXXcb' name can be quite a large number of different values.

Any help would be appreciated.

Thanks,
/Isidor
0
isidor
Asked:
isidor
  • 4
1 Solution
 
knightEknightCommented:
you are trying to set the value of the whole group to 0 ... I don't think you can do that, you can only set the value of individual checkboxes to 0.  ... and if you want that 0 to be posted to the server, you will have to put it in a hidden field by the same name ... either that or set the value of a checked checkbox to 0.
0
 
knightEknightCommented:
try this:

<td>
<input type='checkbox' name='XXXcb' value='1' onclick='unCheckOthers(this);'>
<input type='checkbox' name='XXXcb' value='2' onclick='unCheckOthers(this);'>"
<input type='checkbox' name='XXXcb' value='3' onclick='unCheckOthers(this);'>"
<input type='hidden' name='XXXcb' value="">
</td>




function unCheckOthers(obj)
{
    var objGroup = obj.form[obj.name];
   
    for (var i=0,n=objGroup.length; i<n; i++)
    {
         if (objGroup[i] != obj)
              objGroup[i].checked = false;
    }
}
0
 
knightEknightCommented:
oops, I left out the most important part!  ... but it wasn't going to work as I had thought anyway, so let me play with it and get back to you.
0
 
knightEknightCommented:
ok, here is one way to do it.

Place a hidden field of the same name BEFORE the checkboxes (to give it an index of 0):

   <input type='hidden'   name='XXXcb' value='0'>
   <input type='checkbox' name='XXXcb' value='1' onclick='unCheckOthers(this);'>
   <input type='checkbox' name='XXXcb' value='2' onclick='unCheckOthers(this);'>
   <input type='checkbox' name='XXXcb' value='3' onclick='unCheckOthers(this);'>



then make your function look like this:

function unCheckOthers(obj)
{
   var objGroup = obj.form[obj.name];
   objGroup[0].value="";

   for (var i=1,n=objGroup.length; i<n; i++)
   {
        if (objGroup[i] != obj)
             objGroup[i].checked = false;
        else if ( obj.checked==false )
             objGroup[0].value="0";
   }

 //alert(objGroup[0].value);
}
0
 
isidorAuthor Commented:
Great code, knightEknight. I got it working the way I wanted it to.

Thanks a lot!
/isidor
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now