|
|
Subject:
javascript check/uncheck all checkboxes with a checkbox based on field type.
Category: Computers > Programming Asked by: flyguylol-ga List Price: $5.00 |
Posted:
15 Feb 2006 12:34 PST
Expires: 17 Mar 2006 12:34 PST Question ID: 446259 |
I have a check/uncheck all checkboxes script which is meant for two buttons. I want to apply this code to a checkbox, so that the user can toggle the select/all option. For example, the user checks the selectall checkbox, and all the checkboxes are selected. If the user unchecks the selectall checkbox, all the checkboxes are unselected. The code must check for checkboxes, as the name field for the checkboxes are dynamic. Currently, the code selects all checkboxes, but does not deselect them. Here is the code. function checkAll(obj) { // set the form to look at (your form is called form1) var frm = document.form1 // get the form elements var el = frm.elements // loop through the elements... for(i=0;i<el.length;i++) { // and check if it is a checkbox if(el[i].type == "checkbox" ) { // if it is a checkbox and you submitted yes to the function if(obj == "yes") // tick the box el[i].checked = true; else // otherwise untick the box el[i].checked = false; } } } <input type="checkbox" name="checkbox" value="checkbox" onClick="checkAll('yes');return true;"> | |
| |
|
|
Subject:
Re: javascript check/uncheck all checkboxes with a checkbox based on field type.
Answered By: palitoy-ga on 17 Feb 2006 03:11 PST Rated: |
Hello again and it is nice to see you using Google Answers again! I think I have a solution for you, it may not be the prettiest solution but it does work. This is how I approached it... my first thoughts were how does the check/uncheck all button know what to do? The only solution I could see to this problem is it has to check each of the checkboxes and see if they are checked or not. If they are all checked then the button will uncheck them all, if they are all unchecked the button will check them all. The only drawback to this solution is that if the user has checked one of the boxes the button will not be able to determine what to do and I can see no way around this by using this strategy. What this means is the user will have to click the button twice. If the user has checked any of the boxes, the script will uncheck them all, then when the user clicks the button again all the boxes will become checked. This should be clearer with the example. Try manually checking all the boxes, manually unchecking all the boxes and just checking one (or not all) of the boxes and you should see what I mean. Here is the new function: function checkAll() { // set the form to look at (your form is called form1) var frm = document.form1 // get the form elements var el = frm.elements // a variation to store whether any boxes are checked var testit = false; // loop through the elements... for(i=0;i<el.length;i++) { // check if the element is a checkbox and whether it is checked if (el[i].type == "checkbox" && el[i].checked == true) { // we have found a checked box so remember this! testit = true; } } // loop through the elements again... for(i=0;i<el.length;i++) { // and check if it is a checkbox if(el[i].type == "checkbox" ) { // if it is a checkbox and we have found a checked box if(testit == true) { // tick the box el[i].checked = false; } else { // otherwise untick the box el[i].checked = true; } } } } The code for the button would now become: <input type=button value="(Un)Check All" onClick="checkAll();return false;"> Please let me know if you require any further clarification. |
flyguylol-ga
rated this answer:
and gave an additional tip of:
$5.00
Again, thank-you Palitoy-ga, you're the best! |
|
Subject:
Re: javascript check/uncheck all checkboxes with a checkbox based on field type.
From: pmmbala1976-ga on 15 Feb 2006 14:48 PST |
Hi use this code.... <HTML> <script language=javascript> function checkAll(obj) { var ss var kk kk= obj; ss=document.form1.checkbox.checked; if (ss == false) { kk ="no"; } // set the form to look at (your form is called form1) var frm = document.form1 // get the form elements var el = frm.elements // loop through the elements... for(i=0;i<el.length;i++) { // and check if it is a checkbox if(el[i].type == "checkbox" ) { // if it is a checkbox and you submitted yes to the function //alert(kk); if(kk == "yes") // tick the box el[i].checked = true; else // otherwise untick the box el[i].checked = false; } } } </script> <body> <form name=form1> <input type="checkbox" name="checkbox" value="checkbox" onClick="checkAll('yes');return true;"> <input type="checkbox" name="checkbox1" value="checkbox"> <input type="checkbox" name="checkbox2" value="checkbox"> <input type="checkbox" name="checkbox3" value="checkbox"> </form> </body> </HTML> thanks bala |
Subject:
Re: javascript check/uncheck all checkboxes with a checkbox based on field type.
From: palitoy-ga on 17 Feb 2006 11:14 PST |
Thanks for the 5-star rating, kind comments and tip! They are all appreciated. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |