Using Javascript to select all items in a menu whe

Results 1 to 2 of 2

Thread: Using Javascript to select all items in a menu whe

  1. #1
    Join Date
    Dec 1969

    Default Using Javascript to select all items in a menu whe

    I have a javascript function that I&#039;ve used the in classic asp to select all items in a dropdown menu when a checkbox is checked.<BR><BR>However I can&#039;t get it to work in .NET. I can get the script to run by adding this line to the code behind page_load<BR><BR>select_all.Attributes.Add("OnClic k", "return SelectAll(thecheckbox,theselect)")<BR><BR>However I can&#039;t figure out how to pass in the server controls as objects. Below is the function<BR><BR> &#060;script language="javascript"&#062;<BR>function SelectAll(thecheckbox,theselect){<BR><BR><BR>if (thecheckbox.checked){<BR>for (var i=0; i&#060;theselect.options.length; i++) {<BR>theselect.options[i].selected = true;<BR>}<BR>} else {<BR>for (var i=0; i&#060;theselect.options.length; i++) {<BR>theselect.options[i].selected = false;<BR><BR>}<BR>}<BR>}<BR> &#060;/script&#062;<BR><BR>I guess my real question is what do I put in the code behind in place of thecheckbox,theselect to actually send the objects.<BR><BR>The id&#039;s of the server controls are<BR><BR>checkbox = select_all<BR><BR>dropdown = region_pick<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default Well 'this' is a good start...

    If the onClick is an attribute of the checkbox, then just passing<BR> this<BR>for the checkbox is ideal.<BR><BR>And then for the &#060;SELECT&#062;, if you give it a NAME="whatever", then you could pass<BR> this.form.whatever<BR><BR>So:<BR><BR>select_all.At tributes.Add("OnClick", "return SelectAll(this, this.form.whatever)")<BR><BR>But now simplify the heck out of your JS code:<BR><BR>&#060;script language="javascript"&#062;<BR>function SelectAll(thecheckbox,theselect)<BR>{<BR> for (var i=0; i&#060;theselect.options.length; i++) <BR> {<BR> theselect.options[i].selected = thecheckbox.checked;<BR> }<BR>}<BR>&#060;/script&#062;<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts