[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checkboxlist in Javascript

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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
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.
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).
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

656 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