Solved

Checkboxlist in Javascript

Posted on 2006-11-22
4
1,459 Views
Last Modified: 2009-12-16
Hello,

I have got a checkboxlist in asp.net 2.0 with items populating from Database.

After populating items from Database. I'm insert a new item "Select All"  into to the checkboxlist.

I need actually, a javascript help. When I'm checking the "Select All" checkbox from the checkboxlist it is selecting all the checkboxes inside the checkboxlist and when I'm unchecking "Select All" checkbox javascript function is unchecking all checkboxes in the checkboxlist.  But, when I'm trying to check or uncheck  any checkbox other than "Select All", it is not allowing to check or uncheck.

Here is the javascript function that I'm using.
function CheckUncheckAllItems(checkBoxListId,numOfItems)
{
        var objCtrl;
       
        objCtrl = document.getElementById(checkBoxListId.id + '_' + 0);
         
         
        if (objCtrl != null && objCtrl.checked == true)
        {
             for(i = 0; i < numOfItems; i++)
            {
               
                objItem = document.getElementById(checkBoxListId.id + '_' + i);
             
                if(objItem == null)
                {
                    continue;
                }
                objItem.checked = true;
               
            }      
         }
         
         if (objCtrl != null && objCtrl.checked == false)
         {
             for(i = 0; i < numOfItems; i++)
            {
               
                objItem = document.getElementById(checkBoxListId.id + '_' + i);
               
                if(objItem == null)
                {
                    continue;
                }
                objItem.checked = false;
               
            }
         }
 
}

Please give me a solution as this one of my project requirment. I need it urgently.
Quick response will be highly appriciated
0
Comment
Question by:msasikala
4 Comments
 
LVL 6

Accepted Solution

by:
LeeHenry earned 50 total points
ID: 18001294
Perhaps i am missing something, but why don't you create another function for when they don't select "select all" or "uncheck all", and call then when they select a regular check box.

function CheckUncheckItem(checkBoxListId) {

   var objItem;
       
        objItem = document.getElementById(checkBoxListId.id);
         
         
        if (objItem != null && objItem.checked == true)
        {    
                objItem.checked = true;

         }
         
         if (objItem != null && objItem.checked == false)
         {
         
                objItem.checked = false;
               
            }
         }
0
 
LVL 6

Assisted Solution

by:bigphuckinglizard
bigphuckinglizard earned 50 total points
ID: 18001687
<html><head>Hello</head>
<script>
function doSelectAll() {
   var cb=document.getElementsByTagName('input');
   for (var i=0; i<cb.length; i++)
   {
      if (cb[i].type=="checkbox" && cb[i].id!="cball")
      {
         cb[i].checked=(document.getElementById('cball').checked)
      }
   }
}
</script>
<body>
<form>
<div onclick="document.getElementById('cball').checked=false;">
<input type="checkbox" id="cb1" value="1">1</input><br />
<input type="checkbox" id="cb2" value="2">2</input><br />
<input type="checkbox" id="cb3" value="3">3</input><br />
<input type="checkbox" id="cb4" value="4">4</input><br />
<input type="checkbox" id="cb5" value="5">5</input><br />
<input type="checkbox" id="cb6" value="6">6</input><br />
<input type="checkbox" id="cb7" value="7">7</input><br />
<input type="checkbox" id="cb8" value="8">8</input><br />
<input type="checkbox" id="cb9" value="9">9</input><br />
</div>
<input type="checkbox" id="cball" value="1" onclick="doSelectAll()">Select All</input>

<br />

<input type="text" id="tb1" value="just a random input element that's not a checkbox"></input>
</body></html>
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now