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

trying to understand this javascript code

Status
Not open for further replies.

786snow

Programmer
Nov 12, 2006
75

Hi,

Briefly what the following function does, if all the check boxes in the array of check boxes are checked then it checks the "check_all" checkbox, and if any one of them is checked off then it checks off the "check_all" check box as well

*********


function func_setSelectAll(){
for (i=0;i<array_checkboxesToCheck.length;i++){
bool_allSelected&=document.getElementById(array_checkboxesToCheck).checked;
}
document.getElementById('check_all').checked=bool_allSelected;
bool_allSelected=true;
}

**************************

I understand how it works when all are chcked then "check_all' also gets checked, but I don't know how it is working , if one of them is checked off then "check_all" also gets checked off?

Also what is &=

as they are doing bool_allSelected&=document.getElementById(array_checkboxesToCheck).checked;

 
"x &= y" is the same as saying "x = x & y", i.e. doing a bitwise AND operation. Boolean logic dictates that:

false AND false == false
false AND true == false
true AND false == false
true AND true == true

so if either "x" or "y" are false, the result of "x & y" will be false as well.

AFAIK, "&" does a bitwise AND, whereas "&&" will do a logical AND, which will preserve types on Boolean variables (I could be wrong on this...). In your case, it probably doesn't matter, as you're working with 0s and 1s anyway (true / false == 1 / 0).

So at the end of the day, when that line is executed, if any checkbox is not checked, "bool_allSelected" will be set to false, if it is not already.

Hope this helps,
Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top