Library code snippets

Automatically check/uncheck multiple checkboxes

You must have seen this script working at many places. One that comes to my mind is at Yahoo / Hotmail for checking or unchecking all the mails that are visible on the page. There are lots of places where you can use this script, generally when you want the user to carry out some task on either all or none of the items that you present to him.


<SCRIPT LANGUAGE = "JavaScript">
<!--

function modify_boxes(to_be_checked,total_boxes){
 for ( i=0 ; i < total_boxes ; i++ ){
   if (to_be_checked){  
 document.forms[0].chkboxarray[i].checked=true;
}
   else{
   
document.forms[0].chkboxarray[i].checked=false;
   }
 }  
}
   
-->
</SCRIPT>


<BODY>
<FORM>
<INPUT TYPE=checkbox NAME="chkboxarray" VALUE="1"><br>
<INPUT TYPE=checkbox NAME="chkboxarray" VALUE="2"><br>
<INPUT TYPE=checkbox NAME="chkboxarray" VALUE="3"><br>
<INPUT TYPE=button NAME="CheckAll" VALUE="Check All Boxes" onClick="modify_boxes(true,3)">
<INPUT TYPE=button NAME="UnCheckAll" VALUE="UnCheck All Boxes" onClick="modify_boxes(false,3)">
</FORM>
</BODY>  

Note : The VALUE tag for the checkboxes seem to have no use. But it is required to differentiate between the check boxes when you submit such a form to a server side program. You could differentiate between the checked boxed by giving different VALUEs to the checkbox.

Comments

  1. 28 Oct 2008 at 00:30
    What I need is simple: I’d like a checkbox that UNCHECKS the other checkboxes in a single array. I'd be grateful if someone can suggest a short javascript that would do this without affecting other checkboxes or radio buttons within the same form.
  2. 21 Oct 2008 at 10:06
    hi Susan, Keep the autopost back property of checkbox to true. and whenever a checkobox is checked then use a looping statement(like: for loop) to check the state of all the checkboxs on your form. And keep a counter variable to count the no. of check boxes checked. If the counter increases than 2 then display the message. To scan throught all the checkboxes on your form, lets say the check boxes are on a panel then: foreach(control c in panel.controls) { if(c is checkbox) { checkbox cbx = new checkbox(); cbx = panel.findcontrol(c.id); if(cbx.checked.equals(true) { // increment the counter // check if counter is less than 2 or not // perform the necessary task } } } Regards, Royal
  3. 21 Oct 2008 at 04:36
    Hi! In my program, I have multiple check boxs.(more the 10) But i give rights to select 2 check boxs, If i select more then 2 check boxs, I want to display alert message. Please any one post me modle program in ASP and javascript. Please very very urgent.... any only help me... My javascript program: (Its not working) function myFunction() { var canSu = true; //Check how many checkboxes are on the form boxes = document.frmUpdate.chkboxes.length //Check for all the checkboxes on the form using the loop var chkCnt=0; for (i = 0; i < boxes; i++) { if (document.frmUpdate.chkboxes[i].checked) { chkCnt++; } } if(chkCnt >= 2) { return false; } return true; } By Susan.
  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Automatically check/uncheck multiple checkboxes.

Leave a comment

Sign in or Join us (it's free).

Kiran Pai

Related podcasts

  • jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, Scott Koon, and Steven Harman discuss Microsoft's jQuery in ASP.NET announcement.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are you loo...

Events coming up

  • Dec 8

    December Silicon Valley Ruby Meetup

    Moffett Field, United States

    In a World of Middleware, Who Needs Monolithic Applications? by Jon Crosby With Rack emerging as the standard for composing web applications and services, most recently with Rails adoption, an architectural shift is taking place. Learn how to create next generation web services by reusing existing Rack middleware and supplementing with your own components and micro-frameworks like Sinatra. Bio : Jon likes music, the Open Web, Ruby, Erlang, Haskell, Objective-C, JavaScript and coffee.

Want to stay in touch with what's going on? Follow us on twitter!