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
https://forums.phpfreaks.com/topic/130408-solved-check-all/
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
https://forums.phpfreaks.com/topic/130408-solved-check-all/#findComment-676514
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.