?
Solved

Checkboxlist in Javascript

Posted on 2006-11-22
4
Medium Priority
?
1,465 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 200 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 200 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

765 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