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

Populate textbox based on dropdown selection?

Status
Not open for further replies.

xuanb

Programmer
Apr 9, 2003
29
US
I have a simple registration form with a dropdown menu for courses (eg. 101 Javascript, 202 ASP). I want to automatically write the course date and tuition to a textbox. I have a hunch that I wanna use a switch statement and that i have to pass the option value as a parameter but I'm stuck at the *dirty details*. Here's what I have sofar.

function getTuitionDate(){
if (document.RegFrm.SelectCourse.options[document.RegFrm.SelectCourse.selectedIndex].value != "") {
Switch (course) {
case 101: document.RegFrm.CourseDate.value = "Jan. 1, 1111";
document.RegFrm.Tuition.value = "$1111"; break;

case 102: document.RegFrm.CourseDate.value = "Dec. 9, 9999;
document.RegFrm.Tuition.value = "$9999"; break;
}
}
}

<form action=&quot;formmail.asp&quot; method=&quot;post&quot; name=&quot;RegFrm&quot;>

<select onchange=&quot;getTuitionDate()&quot; id=&quot;SelectCourse&quot; name=&quot;SelectCourse&quot; tabindex=&quot;9&quot;>
<option selected value=&quot;&quot;>Select Course
<option value=&quot;101&quot;>101 Javascript
<option value=&quot;102&quot;>102 ASP
</select>
...
Date: <input name=&quot;CourseDate&quot; type=&quot;text&quot; id=&quot;CourseDate&quot; size=&quot;20&quot; readonly=&quot;true>
Tuition: <input name=&quot;Tuiton&quot; type=&quot;text&quot; id=&quot;Tuition&quot; size=&quot;12&quot; readonly=&quot;true&quot;>

Despite the holiday, anyone out there to help??
 
You could put the dates into an associative array with the course number being the key:

courses=[];
courses[&quot;101&quot;]=[&quot;Jan. 1, 1111&quot;,&quot;$1111&quot;];
courses[&quot;102&quot;]=[&quot;Dec. 9, 9999&quot;,&quot;$9999&quot;];

Then make your getTuitionDate() function look like this:

function getTuitionDate(){
var f=document.RegFrm;
if(f.SelectCourse.selectedIndex>0){
f.CourseDate.value = courses[f.SelectCourse[f.SelectCourse.selectedIndex].value][0];
f.Tuition.value = courses[f.SelectCourse[f.SelectCourse.selectedIndex].value][1];
}
}

Adam
 
Yuhuuu! Working like a charm...Thanks a lot Adam and have a great holiday!
Xuan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top