Jump to content

[SOLVED] check all


jkkenzie

Recommended Posts

<form method="post" name="countries" action="chooseproject.php" name="form">                     
<input type='checkbox' name='American Samoa' value = ' 1'>American Samoa<br />
<input type='checkbox' name='Andorra' value = ' 2'>Andorra<br />
<input type='checkbox' name='Angola' value = ' 3'>Angola<br />
<input type='checkbox' name='Anguilla' value = ' 4'>Anguilla<br />
<input type='checkbox' name='Armenia' value = ' 5'>Armenia<br />
<input type='checkbox' name='Canada' value = ' 6'>Canada<br />
<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.countries.Angola)">
<input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.countries.Angola)">

 

Above is my html:

below is my javascript which is not working:

function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}

 

Instead of my javascript checking Angola only, i would like to check all checkboxes.

My by id, but how do i use id with javascript

 

Thanks

Joe

Link to comment
Share on other sites

if they are the only checkboxes on the form, you can use this:

 

<script type=text/javascript>
function checkAll( form, uncheck )
{
   for (i = 0; i < form.length; i++)
   {
      if(form[i].type == 'checkbox')
         form[i].checked = !uncheck ;
   }
}
</script>

<form method="post" name="countries" action="chooseproject.php" name="form">                     
<input type='checkbox' name='American Samoa' value = ' 1'>American Samoa<br />
<input type='checkbox' name='Andorra' value = ' 2'>Andorra<br />
<input type='checkbox' name='Angola' value = ' 3'>Angola<br />
<input type='checkbox' name='Anguilla' value = ' 4'>Anguilla<br />
<input type='checkbox' name='Armenia' value = ' 5'>Armenia<br />
<input type='checkbox' name='Canada' value = ' 6'>Canada<br />
<input type="button" name="CheckAll" value="Check All" onClick="checkAll(this.form)">
<input type="button" name="UnCheckAll" value="Uncheck All" onClick="checkAll(this.form,true)">
</form>

 

i also condensed it to use just one function

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.