jaymc Posted December 29, 2006 Share Posted December 29, 2006 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 bracketsAny help would be great! Quote Link to comment Share on other sites More sharing options...
jaymc Posted December 29, 2006 Author Share Posted December 29, 2006 Anyone... I thought this was relitavely straight foward...:( Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted December 30, 2006 Share Posted December 30, 2006 [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] Quote Link to comment Share on other sites More sharing options...
jaymc Posted December 31, 2006 Author Share Posted December 31, 2006 Worked a treat, thanks! Quote Link to comment Share on other sites More sharing options...
Destramic Posted January 1, 2007 Share Posted January 1, 2007 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.