Solved

Checkboxlist in Javascript

Posted on 2006-11-22
4
1,461 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

785 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