Javascript select ASP.NET Checkboxlist items

Ok I needed a function that would select certain CheckBoxList items of an ASP.NET CheckBoxList, so here it is, hope is helps someone else out!

if you want I use this function hightlight the item in yellow:

function uStyle(cb) {
   cb.parentNode.style.backgroundColor = cb.checked ? "yellow" : "";
   cb.parentNode.style.color = cb.checked ? "blue" : "";
}

function doCertainOnes(cbControl) {

    var chkBoxList = document.getElementById(cbControl);
    var chkBoxCount = chkBoxList.getElementsByTagName("input");

    chkBoxCount[5].checked = true;
    chkBoxCount[5].focus();    //GOING TO JUMP DOWN IN THE LIST TO THE FIRST SELECTED       
    uStyle(chkBoxCount[5]);
    chkBoxCount[6].checked = true;
    uStyle(chkBoxCount[6]);
    chkBoxCount[7].checked = true;
    uStyle(chkBoxCount[7]);

  
  for (var i = 10; i < 50; i++) {
       chkBoxCount[i].checked = true;
       uStyle(chkBoxCount[i]);
    }
}

I am selecting items (array) 5, 6, 7 and 10 through 49 in the list, now you can repeat the for loop and select different ones to. I happen to know that there will always be 100 items in the checkbox list and which ones they are... I use the "focus()" function to jump to the first item selected in the list...

Use it like this:

<a href="JavaScript:doCertainOnes('<% =CheckBoxList1.ClientID %>');">Select My Favs</a>



posted @ Friday, April 17, 2009 3:02 PM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 2 and type the answer here:
 

Live Comment Preview:

 

Over 13 years of Internet experience and website marketing. Tim Maxey's Tech Blog. Set out to help the lives of others by making their programming easier. No one is the "best" programmer, every good programmer knows that, or should. Always keep learning and if you do not like it, get out of it. Do what makes you happy! My main purpose is to help youth and also anyone for that matter learn they can make a good living in technology!