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

checkAll

Status
Not open for further replies.

dkin

Programmer
Dec 11, 2002
39
ES
How can I reset all the checks in the script below (was originally submitted to a previous thread by Dookie2k2), since If I press some buttons and the click Check All it will check all the others buttons and will remove the selection from the others (mirror response)

<html>
<head>
<script language='javascript'><!--
function checkAll()
{
fLength = document.f1.length;
for (x=0;x<fLength;x++)
{
e = document.f1.elements[x];
if (e.type == 'checkbox')
{
if (e.checked)
{
e.checked = false;
}
else
{
e.checked = true;
}
}
}
}
//--></script>
</head>
<BODY>
<form name='f1'>
Check All<input type='checkbox' onClick='checkAll()'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
<input type='checkbox'><Br>
</form>
</body>
</html>
 
If you want to check or uncheck all the boxes, you don't need to care about their initial values. So I should do like this :
Code:
<html>
<head>
<script language='javascript'><!--
function checkUncheckAll(Check) {
  var boCheck = (check==&quot;C&quot;)
  fLength = document.f1.length;
  for (x=0;x<fLength;x++) {
    e = document.f1.elements[x];
    if (e.type == 'checkbox')
      e.checked = boCheck;
  }
}
//--></script>
</head>
<BODY>
<form name='f1'>
<input type='button' value=&quot;Check All&quot; onClick='checkUncheckAll(&quot;C&quot;)'><Br>
<input type='button' value=&quot;Uncheck All&quot; onClick='checkUncheckAll(&quot;U&quot;)'><Br>
...
..
Water is not bad as long as it stays out human body ;-)
 
Thanks for the Idea, but the first script is not working properly because if I check on of the boxes and then click Check All it will be unchecked at the end of the process.

I still think that the best possibility for fixing the function checkAll() (which is small and cool) is just to reset all the checkbox in the beginning of the function, but unfortunately I do not know how?
 
I keep on saying these two things :
- You'll have problems as long as the control used to call the checkAll function will be a checkbox itself. by the way, checkboxes are usually used as passive controls (they don't act on click or change but their value is read when button is clicked).
- This test is nuts :
Code:
if (e.checked)
  {
  e.checked = false;
  }
else
  {
  e.checked = true;
}
You want to checkAll (it's the function name after all !!!) so why do you code that &quot;if it's checked, then uncheck it&quot;.
Either you replace these lines :
Code:
if (e.type == 'checkbox')
  {
  if (e.checked)
    {
    e.checked = false;
    }
  else
    {
    e.checked = true;
    }
  }
by :
Code:
if (e.type == 'checkbox')
  {
  e.checked = true;
  }
Or you rename your function in &quot;InvertChecks&quot;. Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top