...

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



Chicano
08-21-2002, 11:45 PM
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:

Tip/Suggestion
Personal Message
Complaint

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! =)

boywonder
08-22-2002, 12: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.

regards.

Chicano
08-22-2002, 12: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?

Thanks!

boywonder
08-22-2002, 12: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";
}
//-->
</script>
<form>
<select name="sel" onchange="getOther(this,this.form.oth);">
<option>Tip/Suggestion
<option>Personal Message
<option>Complaint
<option>Other
</select>
<input type="text" name="oth" style="display: none;">
</form>

hope that helps

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

It works like a charm! =)

I really REALLY appreciate your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum