Solved

Checkboxlist in Javascript

Posted on 2006-11-22
4
1,463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
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).

733 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