Forum Moderators: open

Message Too Old, No Replies

Form OnChange

Not a javascript boy, need simple help!

         

bateman_ap

6:34 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My background is ASP and for the life of me have spent the last 4 hours trying to do this simple task!

What I want to do is the following, I have a list of months in a drop down

When someone changes the month I want to refresh the page with a querystring relating to the value they just selected.

At the moment the code is like this:

<form name="formTest" action="test.asp" >
<select name="month">
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
</select>
</form>

I know I have to use OnChange in the select bit but don't know how to make it so it opens the page /test.asp?month=7 if someone selects July.

bateman_ap

7:28 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I should say too I can't submit the form to achive this as I am using this to synamically build another drop down on the same form.

DrDoc

8:11 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<select name="month" onchange="top.location.href='test.asp?month=' + this.options[this.selectedIndex].value">

bateman_ap

8:26 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have no idea how much I am grateful!

bateman_ap

8:52 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, one last question. Is there a simple way to have the new selection automatically selected on the new page. It keeps defaulting back to the first value on the list.

DrDoc

10:01 pm on Jun 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



just add
selected="selected"
to the option you want as default

dcrombie

9:36 am on Jun 14, 2004 (gmt 0)



<FORM method="GET" name="testForm" action="test.asp" > 
<SELECT name="month" onChange="this.form.submit();">
<OPTION value="6">June</OPTION>
<OPTION value="7">July</OPTION>
<OPTION value="8">August</OPTION>
</SELECT>
</FORM>

If you want to set an option to selected using JavaScript, you'll need to:

1) parse the query-string to get the value of "month"; then
2) set document.testForm.months.selectedIndex to the item you want selected (index starts at 0).