Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input type button question 1

Status
Not open for further replies.

calista

Programmer
Jan 24, 2001
545
US
I have created a form where the user may select one or more checkboxes to sign up for mailing lists. That is working OK. What I would like to add is a button the user can click to select all checkboxes, and, when editing their list, deselect all. I would like to then redisplay the form with all boxes checked/unchecked before they hit "Submit". How do I do that? Any suggestions welcome! Calista :-X
Jedi Knight,
Champion of the Force
 
Code:
<SCRIPT LANGUAGE = &quot;JavaScript&quot;>
  <!-- Hide from other browsers

    function checkAll()
    {
      for (var i=0;i<document.myForm.elements.length;i++)
      {
        var e = document.myForm.elements[i];
        if (e.name != 'selectAll')
          e.checked = !e.checked;
      }
    }
  // Stop Hididng from other Browsers  -->
  </SCRIPT>
Change myForm to match the name of your form.

Code:
<input type=&quot;button&quot; value=&quot;Select All!&quot; 

onClick=&quot;return checkAll();&quot;>
Include the onClick function in a button or other check box.

Hope this is what you were looking for!


s-)


 
Thanks, DeZiner! I realized after I posted this question that it was probably a JavaScript thing, about which my knowledge is pretty close to zero. This script swaps unchecked for checked and vica versa. What I would like to do is if the user has some items checked, and wants to update his list to &quot;Check All&quot;, that it would check all unchecked boxes and leave the rest checked. I'd also like to include a &quot;Clear All&quot;, but I assume that is the reverse of the &quot;Check All&quot; script. I'll play with this and see what I can do with it. If I have any other questions, I'll take it to the Javascript forum.

Thanks, again! Calista :-X
Jedi Knight,
Champion of the Force
 
Calista,
I think this might solve your problem (hopefully!). This assumes there are 5 checkboxes though, so you might need to modify the 'for' loop so that it runs through all of your checkboxes. It also assumes the checkboxes are the first items in the form. Let me know if you need some sort of other modification and I'll try something else.

Julie


<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function checkAllBoxes() {
var i;
for (i = 0; i <= 4; i++)
document.myForm.elements.checked = true;
}
//-->
</SCRIPT>

</HEAD>
<BODY>
<form name = &quot;myForm&quot;>
<INPUT type=&quot;checkbox&quot; id=checkbox1 name=checkbox1> Box 1
<BR>
<INPUT type=&quot;checkbox&quot; id=checkbox2 name=checkbox2> Box 2
<BR>
<INPUT type=&quot;checkbox&quot; id=checkbox3 name=checkbox3> Box 3
<BR>
<INPUT type=&quot;checkbox&quot; id=checkbox4 name=checkbox4> Box 4
<BR>
<INPUT type=&quot;checkbox&quot; id=checkbox5 name=checkbox5> Box 5
<BR><BR>
<INPUT type=&quot;button&quot; value=&quot;Check All&quot; id=button1 name=button1 onclick = &quot;checkAllBoxes();&quot;>
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top