| <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> check box select all clear and reselect </title> <Script type = "text/javascript"> // Retrieve the array of all check box objects Function GetAllCheckBox (){ Var div = document. getElementById ("bils "); Var inputs = div. getElementsByTagName ("input "); // Define an array of check boxes to return Var checkboxs = new Array (); Var nIndex = 0; For (var I = 0; I <inputs. length; I ++ ){ // Determine the check box by checking whether the type is checkbox If (inputs [I]. type = "checkbox "){ Checkboxs [nIndex] = inputs [I]; NIndex ++; } } Return checkboxs; } // Select all Function selAll (){ Var checkboxs = GetAllCheckBox (); For (var I = 0; I <checkboxs. length; I ++ ){ Checkboxs [I]. checked = true; } } // Clear all Function clearAll (){ Var checkboxs = GetAllCheckBox (); For (var I = 0; I <checkboxs. length; I ++ ){ Checkboxs [I]. checked = false; } } // Invert Selection Function reverseAll (){ Var checkboxs = GetAllCheckBox (); For (var I = 0; I <checkboxs. length; I ++ ){ If (checkboxs [I]. checked = true ){ Checkboxs [I]. checked = false; } Else { Checkboxs [I]. checked = true; } } } </Script> </Head> <Body> <Div id = "bils"> <Input type = "checkbox" id = "c1"/> <label for = "c1"> football </label> <Input type = "checkbox" id = "c2"/> <label for = "c2"> billiards </label> <Input type = "checkbox" id = "c3"/> <label for = "c3"> Table Tennis </label> <Br/> <Input type = "button" value = "select all" onclick = "selAll ()"/> <Input type = "button" value = "" onclick = "clearAll ()"/> <Input type = "button" value = "invert" onclick = "reverseAll ()"/> </Div> </Body> </Html> |