Re: how to select all checkbox?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: how to select all checkbox?

From: RobG <rgqld@iinet.net.auau>
Date: Mon Aug 01 2005 - 01:16:17 CEST

_andrea.l wrote:
> I have n checkboxes and 1 checkbox 'SELECT ALL'.
> for example:
> <form action="" method="get">
> <input name="sa" type="checkbox" value="v"> select all
> <input name="c1" type="checkbox" value="v"> option 1
> <input name="c2" type="checkbox" value="v"> option 2
> <input name="c3" type="checkbox" value="v"> option 3
> ...
> <input name="cn" type="checkbox" value="v"> option n
> </form>
>
>
> I'd like check all checkboxes when I select 'sa' (select all) and deselect
> all boxes when I select one checkbox (less the checkbox I had selected)
> How can I do that?

<script type="text/javascript">
function checkAll( el, tick ) {
   var els = el.form.elements;
   var x, i = els.length;
   while ( i-- ) {
     x = els[i];
     if ( 'input' == x.nodeName.toLowerCase() &&
          'checkbox' == x.type ) {
       x.checked = tick;
     }
   }
}

function checkThis( el ) {
   checkAll( el, false );
   el.checked = true;
}

</script>

<form action="">
  <input name="sa" type="checkbox" value="v" onclick="
    checkAll(this, true);
  "> select all<br>
  <input name="c1" type="checkbox" value="v" onclick="
    checkThis(this);
  "> option 1<br>
  <input name="c2" type="checkbox" value="v" onclick="
    checkThis(this);
  "> option 2<br>
  <input name="c3" type="checkbox" value="v" onclick="
    checkThis(this);
  "> option 3<br>
  <input name="cn" type="checkbox" value="v" onclick="
    checkThis(this);
  "> option n<br>
  <input type="reset">
</form>

-- 
Rob
Received on Tue Oct 18 03:03:57 2005