Jump to content

Check boxes from database, javascript check all function...ehh


squiggerz

Recommended Posts

OK, I found this javascript code (below) that takes a "Check All" button and checks every checkbox in a group. My problem is where it does the onClick="checkAll(document.myform.list) <-- my 'list' is set up with input names like this 'list[]' because it posts the values checked into an array that I later combine to form a string. but the [] seems to prevent the javascript from working as when I drop the [] from my checkboxes, the javascript works (my post doesnt of course). I'm a complete moron when it comes to javascript and I know this is a php forum, but I'm just hoping somebody here has dealt with this in the past and knows the easy way around.

 

<SCRIPT LANGUAGE="JavaScript">
// <input type=button name="CheckAll"   value="Check All" onClick="checkAll(document.myform.list)">
// <input type=button name="UnCheckAll" value="Uncheck All" onClick="uncheckAll(document.myform.list)">

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 ;
}
</script>

Hi

 

I dont know if I understand your question fully, but ill give it a go.

 

My first attempt...

You could make you functions take two arguments.

First argument would be the form you working on, and the second would be the list of checkboxes

 

// <input type=button name="CheckAll"   value="Check All" onClick="checkAll(document.myform , A LIST OF THE FIELDS)">

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

 

Second attempt

Just pass all the id's to the function like so

 

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

 

I hope this help

Thanks Daniel

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.