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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox Select/Deselect All Problem

Status
Not open for further replies.

DH

Programmer
Dec 8, 2000
168
0
0
I am using the following Javascript shown below to check or uncheck all checkboxes with the id 'DeleteThis' in a asp.net datagrid. This functionality allows the user to select various records to be deleted for example.

The problem is that I also have a radio button list named 'searchby' on the same form. When I use check the select_deselectAll function it also changes the value of the 'searchby' radio list. How can I tell the select_deselectAll to ignore the 'searchby' radio list and only check/uncheck the checkboxes within the datagrid with the id of 'DeleteThis'?

I am not very familar with Javascript and any help will be very appreciated!

Javascript:
Code:
<script language=JavaScript>
<!--

/*Using modified select_deselectAll script function of my original one, from Developerfusion.com forum members - ketcapli & thombo Forum Post - [[URL unfurl="true"]http://www.developerfusion.co.uk/forums/topic-22773[/URL]]*/function select_deselectAll (chkVal, idVal) {
 var frm = document.forms[0];
 if (idVal.indexOf('DeleteThis') != -1 && chkVal == true){
 var AllAreSelected = true;
 for (i=0; i<frm.length; i++) {
 if (frm.elements[i].id.indexOf('DeleteThis') != -1 && frm.elements[i].checked == false){ 
 AllAreSelected = false;
 break;
 } 
 } 
 if(AllAreSelected == true){
 for (j=0; j<frm.length; j++) {
 if (frm.elements[j].id.indexOf ('CheckAll') != -1) {
 frm.elements[j].checked = true;
 break;
 }
 }
 }
 } else {
 for (i=0; i<frm.length; i++) {
 if (idVal.indexOf ('CheckAll') != -1) {
 if(chkVal == true) {
 frm.elements[i].checked = true; 
 } else {
 frm.elements[i].checked = false; 
 }
 } else if (idVal.indexOf('DeleteThis') != -1 && frm.elements[i].checked == false) {
 for (j=0; j<frm.length; j++) {
 if (frm.elements[j].id.indexOf ('CheckAll') != -1) { 
 frm.elements[j].checked = false;
 break; 
 } 
 } 
 } 
 } 
 } 
 } 

//--> 

</script>
Thank you.

DH
 
The conditionals in the function are very much defective in the function.

If you have any radio button group or anything like text boxes, then you will always get AllAreSelected evaluated to false, because input text elements will always have .checked evaluated to false and that radio button group will encounted checked false except one at most. Hence this block will result in AllAreSelected assigned to false.
[tt]
if (frm.elements.id.indexOf('DeleteThis') != -1 && frm.elements.checked == false){
AllAreSelected = false;
break;
}
[/tt]
Other than that, there might be additional flaws. But, it seems vain in guessing, like what is "CheckAll" element and its role to play.
 
As cLFlaVA said is a similar thread, thread216-1170056
Your ID tags should always be unique.
Try something like this.
Code:
function checkall() {
  var myboxes = document.forms['myform'].elements['DeleteThis'];
  for (var x=0;x<myboxes.length;x++)
  {
    myboxes[x].checked = true;
  }
}

This retrieves all elements that have the name DeleteThis as an array you can loop through.


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top