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>

Link to comment
Share on other sites

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

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.