Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Asp.Net Datagrid and Check All Checkboxes 1

Status
Not open for further replies.

checkai

Programmer
Jan 17, 2003
1,629
US
I have a datagrid with 2 columns of checkboxes. The one column has a header checkbox with the following "check all" code.
Code:
function checkall(chk){
 //check all checkboxes
 var checkValue = chk.checked;
 if ( chk.checked ){
  chk.checked = false;
 } else {
  chk.checked = true;
 }
    
 count = document.forms[0].elements.length;
 for (i=0; i < count; i++) {
  if ( document.forms[0].elements[i].type.toLowerCase() == "checkbox") {
    document.forms[0].elements[i].checked = checkValue;
  }
 }
}

The problem is, is that this code is checking all checkboxes on the page. However, I only want the one column of checkboxes to get affected. Is there an attribute that I can check to do this? I was thinking I could check against the name of the checkbox with something like this...however, I wasn't sure if there is a like type statement in JS

Code:
document.forms[0].elements[i].ID.toLowerCase().contains("chx")

"...your mom goes to college..."
 
like" is "indexOf"...

Code:
if ( document.forms[0].elements[i].ID.toLowerCase().indexOf("chx") > -1 )

indexOf will return -1 if the string cannot be found in the comparison string, otherwise it will return the starting position, with the first position being 0.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Awesome! Thanks...Here's what has worked:
Code:
function checkall(chk){
 //check all checkboxes
 var checkValue = chk.checked;
    
 count = document.forms[0].elements.length;
 for (i=0; i < count; i++) {
  if ( document.forms[0].elements[i].type.toLowerCase() == "checkbox" &&
     ( document.forms[0].elements[i].id.toLowerCase().indexOf("chkx") > -1 || 
       document.forms[0].elements[i].id.toLowerCase().indexOf("chkall") > -1 )) {
   document.forms[0].elements[i].checked = checkValue;
  }
 }
}

"...your mom goes to college..."
 
glad it worked. may i suggest some easier-to-read code?

Code:
function checkall(chk){
 //check all checkboxes
 var checkValue = chk.checked;
 
 var e = document.forms[0].elements;

 for (i=0; i < e.length; i++) {
  if ( e[i].type.toLowerCase() == "checkbox" &&
     ( e[i].id.toLowerCase().indexOf("chkx") > -1 ||
       e[i].id.toLowerCase().indexOf("chkall") > -1 )) {
       e[i].checked = checkValue;
  }
 }
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top