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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using select box to change text field values

Status
Not open for further replies.

Ssanai80

IS-IT--Management
Jun 28, 2001
21
0
0
US
here is my code to use select box to change other text fields in the same form. I can't figure out why it's not working. it would be great if you can help me out. basically the select button allows you to choose an apartment type. and other text fields representing various features of the chosen apartment type should be filled with relevent info when select box changes.

sample code:
<FORM ACTION=&quot;Apartment_search.asp&quot; METHOD=&quot;POST&quot; name=&quot;information&quot;>
<TABLE WIDTH=&quot;300&quot; BORDER=&quot;1&quot; bgcolor=&quot;#C0C0C0&quot; bordercolor=&quot;#696969&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>

<!-- This is the javascript for apt. search menu -->
<script language=&quot;javascript&quot;>

function setvals() {
if ( document.information.apt_type.value == 1 ) {
document.information.Bedroom.value = &quot;1&quot;;
document.information.Bathroom.value = &quot;1&quot;;
} else if ( document.information.apt_type.value == 2 ) {
document.information.Bedroom.value = &quot;1&quot;;
document.information.Bathroom.value = &quot;2&quot;;
} else {
}
}
</script>


<TR>
<TD VALIGN=&quot;Top&quot;><b><u>Apartment Type</u></b></TD>
<TD VALIGN=&quot;Top&quot;>
<select name=&quot;apt_type&quot; onChange=&quot;setvals()&quot;>
<option value=&quot;0&quot;>-- None --</option>
<option value=&quot;1&quot;>Studio Reg</option>
<option value=&quot;2&quot;>Studio Prem</option>
</select>
</TD>
</TR>
<TR>
<TD VALIGN=&quot;Top&quot;><b><u>Bed Room</U></b></TD>
<TD VALIGN=&quot;Top&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;Bedroom&quot; size=&quot;2&quot; maxlength=&quot;2&quot;></TD>
</TR>
<TR>
<TD VALIGN=&quot;Top&quot;><b><u>bathroom</u></b></TD>
<TD VALIGN=&quot;Top&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;Bathroom&quot; size=&quot;2&quot; maxlength=&quot;2&quot; value=&quot;&quot;></TD>
</TR>
<TR>
<TD VALIGN=&quot;Top&quot;></TD>
<TD VALIGN=&quot;Top&quot;><INPUT TYPE=&quot;submit&quot; VALUE=&quot;Search&quot;></TD>
</TR>
</TABLE>
</FORM>
 
you are refrencing to your select box wrong, a common error.

to get the currently selected value:

document.form.select.options[document.form.select.selectedIndex].value

selectedIndex and the options array are the important aspects here. the options array is just an array of the objects of the selects items. selectedIndex is just the array index of the currently selected item.

knowing that, this should work for you:

function setvals() {
var currentSelect=document.information.apt_type.options[document.information.apt_type.selectedIndex];
if ( currentSelect.value == 1 ) {
document.information.Bedroom.value = &quot;1&quot;;
document.information.Bathroom.value = &quot;1&quot;;
} else if ( currentSelect.value == 2 ) {
document.information.Bedroom.value = &quot;1&quot;;
document.information.Bathroom.value = &quot;2&quot;;
} else {
}
} Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top