Solved

How can I check all items in asp.net checkboxlist control using javascript?

Posted on 2007-11-15
9
3,027 Views
Last Modified: 2008-02-01
Experts when I click a hyper link I would like to either: check all or un check all items in a asp.net checkboxlist control.  I found some javascript that works, but it checks all checkboxs on the form, I just want the items in the checkboxlist.  I tried modifying the javascript to just point to the asp.net control, but I am missing something.  The commented out code works to check or uncheck all checkboxes.

      <script language="javascript">
      function Select(Select){    
      //for (var n=0; n < document.forms[0].length; n++)        
      //if (document.forms[0].elements[n].type=='checkbox')          
       //document.forms[0].elements[n].checked=Select;    
       //return false;  
       for (var n=0;n < document.getElementById("chkstations").length; n++)
       (document.getElementById("chkstations").elements[n].checked=Select;
       return false;
       }
            </script>

Html hyperlink to check or uncheck boxes
<a name="station_select"></a>Select <a href="#station_select" onclick="javascript:Select(true)">
All</a> | <a href="#station_select" onclick="javascript:Select(false)">None</a>
                                                                                                                                                
0
Comment
Question by:tentavarious
[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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:craskin
ID: 20289726
i only know how to do this with a postback (so maybe it's not very useful to you).
For i As Integer = 0 to myCheckBox.Items.Count - 1
     myCheckBox.Items(i).Checked = True
Next

Open in new window

0
 

Author Comment

by:tentavarious
ID: 20289772
I know it can be done with javascript, because I am able to check all asp.net checkboxes, I would like to stick with javascript.
0
 
LVL 12

Expert Comment

by:craskin
ID: 20289860
how about
function selectAll (listId)
{
for (i = 0; index < chkStations.length; i++)
{
var currentCheckBox = chkStations [i];
 
if (currentCheckBox != null)
{
var currentCheckBoxId = currentCheckBox.id;
 
var lastUnderscoreIndex = currentCheckBoxId.lastIndexOf ('_');
var currentCheckBoxIdBaseId =
currentCheckBoxId.substring (0, lastUnderscoreIndex);
 
if (currentCheckBoxIdBaseId == listId)
{
currentCheckBox.checked = true;
}
}
}
}

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 12

Expert Comment

by:craskin
ID: 20289877
wait, that's not gonna work.

try without specifying chkStations in the for loop and just use checkBoxes
0
 
LVL 10

Accepted Solution

by:
Oliver Amaya earned 125 total points
ID: 20289917
Hi, try this and see if it works:

function Select(Select, checkBoxListID){    
  var checkBoxList = document.getElementById("checkBoxListID");
  var checkBoxes = checkBoxList.getElementsByTagName("input");
 
  for(var i = 0; i < checkBoxes.length; i++) {
    checkBoxes[i].checked = Select;
  }
}

Open in new window

0
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20289928
Sorry a little syntax error


// This
var checkBoxList = document.getElementById("checkBoxListID");
 
// Should be
var checkBoxList = document.getElementById(checkBoxListID);

Open in new window

0
 

Expert Comment

by:timparis
ID: 23545002
Hi, I'm trying to do the exact same thing as written up in this post. I'm getting a "Microsoft JScript runtime error: 'null' is null or not an object" error.
checkBoxList in the statement below is being set to null.
var checkBoxList = document.getElementById(checkBoxListID);
I noticed in the page source the id of the input is changed from CheckBoxList1 to
 SessionControl_CheckBoxList1_0
Do I need to handle this somehow?
I'm calling the function like this: Select <a href="#" onclick="javascript:Select(true, 'CheckBoxList1')">All</a>
Is that correct?

Thanks.
0
 

Author Comment

by:tentavarious
ID: 23548236
You would need to modify the function to accept another parameter, because I am only passing one you are passing two.  Also make sure the control id is correct,  javascript, I believe is case sensitive.
0
 

Expert Comment

by:timparis
ID: 23549475
I'm calling the funtion described in the post. Here's my code. An other set of eyes would be helpful, Thanks. I've tried   document.getElementByName   and   document.getElementById   same results. CheckBoxList is null.

<asp:CheckBoxList ID="FunctionalCheckBoxList" runat="server"
    DataSourceID="LinqFunctionalAreas" DataTextField="functionName"
    DataValueField="functionId" RepeatColumns="3" RepeatDirection="Horizontal"
    onselectedindexchanged="FunctionalCheckBoxList_SelectedIndexChanged">
</asp:CheckBoxList>

<script language="javascript" type="text/javascript">
    function Select(Select, checkBoxListID) {
    var checkBoxList = document.getElementByName(checkBoxListID);
    var checkBoxes = checkBoxList.getElementsByTagName("input");
    for (var i = 0; i < checkBoxes.length; i++) {
        checkBoxes[i].checked = Select;
    }
}
</script>

Select <a href="#" onclick="javascript:Select(true, 'FunctionalCheckBoxList')">All</a> | <a href="#" onclick="javascript:Select(false, 'FunctionalCheckBoxList')">None</a>
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

631 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