469,927 Members | 1,654 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

javascript checkall not working

I am trying to get a checkall/uncheckall working for my form with javascript on an apache web server. I know I am doing something wrong and can not get the proper syntax for the javascript to work.

thanks for the help.


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script LANGUAGE="JavaScript">
  4. function doIt(v)
  5. {
  6.  
  7. document.f1.c3.checked=false;
  8. document.f1.c4.checked=false;
  9.  
  10. document.f1.seq23923.checked=val(v);
  11. document.f1.seq23925.checked=val(v);
  12. document.f1.seq23938.checked=val(v);
  13. }
  14. </script>
  15. </head>
  16.  
  17. <body>
  18.  
  19. <Form name=f1 >
  20.  
  21. <input type="checkbox" name="c3" value="false" onclick="doIt(this.value)">All Not Checked<BR>
  22. <input type="checkbox" name="c4" value="true" onclick="doIt(this.value)">All Checked<BR>
  23. <hr>
  24. <input type="checkbox" name="seq" value="23923">23923<br>
  25. <input type="checkbox" name="seq" value="23925">23925<br>
  26. <input type="checkbox" name="seq" value="23938">23938<br>
  27.  
  28.  
  29. </form>
  30.  
  31. </body>
  32. </html>
  33.  
Nov 24 '06 #1
4 1711
Here you're passing variable "v" to function Val() which doesn't exist in the script.

Expand|Select|Wrap|Line Numbers
  1. document.f1.seq23923.checked=val(v);
  2. document.f1.seq23925.checked=val(v);
  3. document.f1.seq23938.checked=val(v);
  4.  
I'm not sure what the script is supposed to do when it works properly. Could you provide more information?
Nov 24 '06 #2
I am trying the get my checkbox to be checked or unchecked the checkbox name 'seq'. I am not careing about the named checkedboxes c3, or c4.
Nov 24 '06 #3
This should be close to what you're looking for:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. <script type="text/javascript">
  8. function uncheck()
  9. {
  10.     // the elements whose names are "seq" are stored in an array
  11.     var seqArray = document.getElementsByName("seq");
  12.  
  13.     // if the elements exist
  14.     if(seqArray)
  15.     {
  16.         // loop through the elements sequentially
  17.         for(var i = 0; i < seqArray.length; i++)
  18.         {
  19.             // if the particular box is checked, uncheck it
  20.             if(seqArray[i].checked == true)
  21.             {
  22.                 seqArray[i].checked = false;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. function check()
  29. {
  30.     var seqArray = document.getElementsByName("seq");
  31.  
  32.     if(seqArray)
  33.     {
  34.         for(var i = 0; i < seqArray.length; i++)
  35.         {
  36.             if(seqArray[i].checked == false)
  37.             {
  38.                 seqArray[i].checked = true;
  39.             }
  40.         }
  41.     }
  42. }
  43. </script>
  44. <title>Untitled Document</title>
  45. </head>
  46.  
  47. <body>
  48.  
  49. <form name="f1">
  50. <input type="checkbox" name="c3" onclick="uncheck()">All Not Checked<BR>
  51. <input type="checkbox" name="c4" checked="checked" onclick="check()">All Checked<br />
  52. <hr />
  53. <input type="checkbox" name="seq" checked="checked" value="23923">23923<br>
  54. <input type="checkbox" name="seq" checked="checked" value="23925">23925<br>
  55. <input type="checkbox" name="seq" checked="checked" value="23938">23938<br>
  56. </form>
  57.  
  58. </body>
  59. </html>
  60.  
  61.  
One thing to note is that to make a checkbox checked by default, use check="checked" within the input element.

There's no need to pass the All Not Checked and All Checked input elements because you're not testing for them. Just get what you want to modify, not what's doing the modifying.

You also might consider making the All Not Checked and All Checked boxes radio buttons, since it only makes since for only one to be checked at a time.

Good luck!
Nov 25 '06 #4
WOW! I was wondering why everybody was put loops for the checkall stuff. I though I did not want a loop, because my values for the checkboxes where large and getting larger. I did an alert to look at some of the variables and then it clicked.


Thanks,
Nov 25 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by chadlupkes | last post: by
4 posts views Thread by Mike P | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.