Tim Maxey .NET Technology Blog & Resources
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>