View Single Post
Old 01-20-2013, 10:29 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Silly code.

This:
Code:
    checkname[i].checked = exby.checked? true:false
should be shortened to just
Code:
    checkname[i].checked = exby.checked;
Also very poor code. Doesn't use { ... } around the for loop statements. Doesn't put a semicolon at the end of each statement. Doesn't declare the variable i. Would make me want to avoid code from that site.

*********

Anyway, two answers:

(1) Put that code at the *END* of your HTML, not in the <head> section.
And then just add
Code:
checkAll(document.vspecs.equip,document.vspecs.specall);
to the end of it.

That is:
Code:
<html>
<body>
<form name="vspecs">
<input type="checkbox" name="specall" checked 
       onclick="checkAll(document.vspecs.equip,this)"> Select All
<input type="checkbox" name="equip" value="aride"> Air-Ride
...
</form>
<script type="text/javascript">
/* This script and many more are available free online at
The JavaScript Source!! http://www.javascriptsource.com
Created by: Hillel Aftel | */
function checkAll(checkname, exby) {
    for (i = 0; i < checkname.length; i++)
    {
        checkname[i].checked = exby.checked;
    }
}
checkAll(document.vspecs.equip,document.vspecs.specall);
</script>
</body>
</html>
(2) The better answer:
To begin with, named forms are obsolete. Use an ID for your form instead.

And then write the code using UNOBTRUSIVE JavaScript:
Code:
<!DOCTYPE html>
<html>
<body>
<form id="vspecs">
<label><input type="checkbox" name="specall"/> Select All </label>
<label><input type="checkbox" name="equip" value="aride"/> Air-Ride</label>
...
</form>
<script type="text/javascript">
(
  function()
  {
      var form = document.getElementById("vspecs");
      form.specall.onclick = checkall;

      function checkall() 
      {
          for (var i = 0; i < form.equip.length; i++)
          {
              form.equip[i].checked = form.specall.checked;
          }
      }
      // and checkall to get started:
      form.specall.checked = true;
      checkall();
  }
)(); 
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
Rcoleman25 (01-21-2013)