Jump to content

[SOLVED] Check Box Check All


jaymc

Recommended Posts

I have the following javascript

[code]<SCRIPT LANGUAGE="JavaScript">
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>[/code]

And I have the following HTML

[code]<form name="myform" action="checkboxes.asp" method="post">
<input type="checkbox" name="list[]" value="1">Java<br>
<input type="checkbox" name="list[]" value="2">Javascript<br>

<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list)">
</form>[/code]

That works but only when each checkbox name is set as [b]name=list[/b]

I need the name to be set as [b]name=list[][/b] for use with a PHP array.

Ive tried changing the following line

[code]<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list)">[/code]

to

[code]<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list[])">[/code]

But it wont work. Just wondering if their is a way around this? Doesnt appear to like those square brackets

Any help would be great!

Link to comment
Share on other sites

[code]function checkAll(f) {
for (i = 0 ; i < f.elements.length; i++) {
if ((f.elements[i].type == "checkbox") && (f.elements[i].name == "list[]")) {
f.elements[i].checked = f.CheckAll.checked;
}
}
return true;
}
[/code]

[code]<form....
<input type="checkbox" name="CheckAll" value="" onclick="checkAll(this.form)" />Check All
</form>[/code]
Link to comment
Share on other sites

ive been working on one..this may help you out one function for it all :D

[code]
<SCRIPT>
var checkbox_state = true;

function toggle_checkboxes(the_form)
{
var button        = document.getElementById("toggle_checkboxes");
var elements      = the_form.parentNode.elements;

for (var i = 0; i < elements.length; i++)
{
var element      = elements[i];
var type          = element.type == "checkbox";

if (type)
{
element.checked = checkbox_state;
}
}

if (checkbox_state)
{
checkbox_state = false;
button.innerHTML = "<b>Unselect All</b>";
}
else
{
checkbox_state = true;
button.innerHTML = "<b>Select All</b>";
}
}
</SCRIPT>
<META content="MSHTML 6.00.2900.2995" name=GENERATOR></HEAD>
<BODY>
<FORM id=edit_remove name=edit_remove action=page.php method=post>
<INPUT class=check id=row[] type=checkbox value=Yes name=1><BR>
<INPUT class=check id=row[] type=checkbox value=Yes name=2><BR>
<INPUT class=check id=row[] type=checkbox value=Yes name=row[]><BR>
<INPUT class=check id=row[] type=checkbox value=Yes name=row[]><BR>
<INPUT class=check id=row[] type=checkbox value=Yes name=row[]><BR>
<div id="toggle_checkboxes" onclick="toggle_checkboxes(this);"><b>Select All</b></div>
</FORM>
[/code]
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.