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

Select All Checkboxes?

Status
Not open for further replies.

trojan800

Programmer
Nov 27, 2001
53
US
Ok here is my problem. I have an array of checkboxes created dynamically from the database. I have a button that when clicked I want it to select all the checkboxes on the form. I realize this should be a fairly easy and seems to be quite common as I have already used the search tab to do some research and as far as I can tell, what I have looks ok. However this is not saying much. So please if anyone can add some insight it would be greatly appreciated. My code is as follows:

<p>    <input type=&quot;button&quot; onClick=&quot;SelectAll()&quot; value=&quot;Select All&quot; name=&quot;SelectAll&quot;></p>

function SelectAll()
{
var i;
var form = document.FORM1;

for(i = 0; i < form.elements.length; i++)
{
  if(form.elements.type == &quot;checkbox&quot;)
{
        form.elements.checked = true;
}
}

}

the error at the bottom of my browser(NS)is as follows :
Javascript error: Type 'javascript:' into Location for details.
I am not sure what this error means either. Does anyone know what location to type 'javascript:' in, cause I sure would like some details.

Thanks again - Rory
 
You type in &quot;javascript:&quot; in the address bar...the same place where you enter the URL addresses... I have not failed; I merely found 100,000 different ways of not succeding...
 
i think all you would have to do to make it work is change the name of the button to something else. i don't think you can have a button and a function named the same thing. at least that's all i changed and it worked fine.
 
1. - as Discord had mentioned, do not name different things wiuth the same name. Javascript will become confused when executing your script what it is exactly you reffering to.
2. do not name your form &quot;form&quot; same thing - javascript might become confused.
3. put document.form_name_here.blah blah NS does not seem to like for the script to refer to the form without the document part in front. --------------------------------------------------
Goals are dreams with deadlines
 
Thanks everyone... I am slowly learning. I did as you all suggested.

I changed the function name to SelectCheckBox as well as the reference from the button call. However (after learning where to type javascript...thanks!) I am getting the error: SelectCheckBox is not defined. How is this not defined and where do I define it? Please more help. TIA. - Rory

here is the code as it looks now:
<p>    <input type=&quot;button&quot; onClick=&quot;SelectCheckBox()&quot; value=&quot;Select All&quot; name=&quot;SelectAll&quot;></p>
function SelectCheckBox()
{
var i;
var form = document.FORM1;

for(i = 0; i < form.elements.length; i++)
{
  if(form.elements.type == &quot;checkbox&quot;)
{
        form.elements.checked = true;
}
}

}
 
trojan800, try this:
Put the function in the <head> section, like this:

Code:
<head>
<script>
function SelectCheckBox() {
  var i;
  var form = document.FORM1;
        
  for(i = 0; i < form.elements.length; i++) {
    if(form.elements[i].type == &quot;checkbox&quot;) {
      form.elements[i].checked = true;
    } 
  }    
}
</script>
</head>

Then in the <body>, in the <form>, you would have:

<p><input type=&quot;button&quot; onClick=&quot;return SelectCheckBox()&quot; value=&quot;Select All&quot; name=&quot;SelectAll&quot;></p>

Make sure that form name is exactly as FORM1... I have not failed; I merely found 100,000 different ways of not succeding...
 
Thanks GUJUm0deL for the advice. It does look weird that I posted the button before the script. Thought it would be more clear at the time, I realize that it is not. Everything is how you suggested along with the form name being FORM1. Please anything else. Thanks - Rory
 
trojan800, try this:


Code:
<html>
<head>

<SCRIPT LANGUAGE = &quot;JavaScript&quot;>
function checkboxesss(check_all_boxes,all_the_boxes){ 
  for ( i=0 ; i < all_the_boxes ; i++ ){ 
    if (check_all_boxes){  
  document.forms[0].chkbx[i].checked=true; 
} 
    else{ 
    
 document.forms[0].chkbx[i].checked=false; 
    } 
  }   
}
     
-->
</SCRIPT> 
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<FORM>
<INPUT TYPE=checkbox NAME=&quot;chkbx&quot; VALUE=&quot;1&quot;><br>
<INPUT TYPE=checkbox NAME=&quot;chkbx&quot; VALUE=&quot;2&quot;><br>
<INPUT TYPE=checkbox NAME=&quot;chkbx&quot; VALUE=&quot;3&quot;><br>
<INPUT TYPE=checkbox NAME=&quot;chkbx&quot; VALUE=&quot;4&quot;><br>
<INPUT TYPE=checkbox NAME=&quot;chkbx&quot; VALUE=&quot;5&quot;><br>
<INPUT TYPE=checkbox NAME=&quot;chkbx&quot; VALUE=&quot;6&quot;><br>
<INPUT TYPE=button NAME=&quot;CheckAll&quot; VALUE=&quot;Check All Boxes&quot; onClick=&quot;checkboxesss(true,6)&quot;>
<INPUT TYPE=button NAME=&quot;UnCheckAll&quot; VALUE=&quot;UnCheck All Boxes&quot; onClick=&quot;checkboxesss(false,6)&quot;>
</FORM>
</BODY>  
</html>

Ok now here: <INPUT TYPE=button NAME=&quot;CheckAll&quot; VALUE=&quot;Check All Boxes&quot; onClick=&quot;checkboxesss(true,6)&quot;>
<INPUT TYPE=button NAME=&quot;UnCheckAll&quot; VALUE=&quot;UnCheck All Boxes&quot; onClick=&quot;checkboxesss(false,6)&quot;>
, you can change the number to any number to check only those boxes...as a trial, instead of 6 type in 4 and see what happens...
This is what you wanted right?? Unless I missed something... I have not failed; I merely found 100,000 different ways of not succeding...
 
Thanks again GUJUm0deL. I tried it your way and I am still getting the error:
checkboxesss is not defined.
WHY?? My other functions work fine when called from onSubmit. I am totally lost. Unless someone can point me in right direction. Help! - Rory
 
Still getting that message?? What browser are you using?? Is it IBM or MAC?? I tried my code in both IE6 and NS4 on a WIN98 pc and worked like a charm...
Hmmm, you copied and pasted the code I gave and ran it without changing anything right?? I have not failed; I merely found 100,000 different ways of not succeding...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top