View Full Version : Textbox to appear when Option in dropdown menu is clicked.

08-22-2002, 12:45 AM
Hi. I am making a simple form which sends an e-mail back to me... and you can select a Subject from a drop down menu, things like:

Personal Message

and then there will be a
"Other" option as well... but when you click on Other, a textbox is to appear next to the Select Box, where you can type your own Subject.

Now, I have been trying to do stuff like naming the textbox with an ID of "MyText" and giving it a style="display:none" then on the Other option doing
<option onSelect="MyText.style.display=''">Other</option>

(and also tried OnClick) yet none of this works.

Any tips?

Thanks a lot! =)

08-22-2002, 01:09 AM
try onchange in the select tag,
you'd need to determine which option is selected (selectedIndex) at that point and if it's the "other" option show the field, else hide it. I can dig up some working examples if you need more info.


08-22-2002, 01:19 AM
A few examples would be very helpful... I understand what you mean, by the selectedIndex and stuff, but how can I determine which one is selected at the moment?


08-22-2002, 01:23 AM
Here you go - I tweaked a function I had to suit your purposes.

Note: this function is designed to be re-used elsewhere and it also allows you add options without worrying about messing up the code... however it requires that the 'other' option is always last. Not necessary of course - just one way to do this.

<script language="JavaScript" type="text/javascript">
function getOther(sel,fld){
fld.style.display = (sel.selectedIndex==sel.options.length-1)?"inline":"none";
<select name="sel" onchange="getOther(this,this.form.oth);">
<option>Personal Message
<input type="text" name="oth" style="display: none;">

hope that helps

08-22-2002, 01:53 AM
Thanks so much!

It works like a charm! =)

I really REALLY appreciate your help.