PDA

View Full Version : making a selection that creates a text field


Try_Try_Again
11-14-2002, 08:44 AM
first post...thank God something like this exists.

I'm trying to work with the below code (http://www.cs.tut.fi/~jkorpela/forms/combo.html) but it's not exactly what I need.

The code below only allows the last choice to bring up a text field. I actually need a drop down menu that has 7 choices; 2 that don't need a field and 5 that do need a field to appear.

Any help would be much appreciated.


<TITLE>Drop down menu with "other" option</TITLE>
<script type="text/javascript" language="JavaScript"><!--
function activate(field) {
field.disabled=false;
if(document.styleSheets)field.style.visibility = 'visible';
field.focus(); }
function last_choice(selection) {
return selection.selectedIndex==selection.length - 1; }
function process_choice(selection,textfield) {
if(last_choice(selection)) {
activate(textfield); }
else {
textfield.disabled = true;
if(document.styleSheets)textfield.style.visibility = 'hidden';
textfield.value = ''; }}
function valid(menu,txt) {
if(menu.selectedIndex == 0) {
alert('You must make a selection from the menu');
return false;}
if(txt.value == '') {
if(last_choice(menu)) {
alert('You need to type your choice into the text box');
return false; }
else {
return true; }}
else {
if(!last_choice(menu)) {
alert('Incompatible selection');
return false; }
else {
return true; }}}
function check_choice() {
if(!last_choice(document.demoform.menu)) {
document.demoform.choicetext.blur();
alert('Please check your menu selection first');
document.demoform.menu.focus(); }}
//--></script>

<form action="chucknut.php"
name="demoform" onsubmit=
"return valid(this.menu,this.choicetext)">
<select name="menu" onchange=
"process_choice(this,document.demoform.choicetext)">
<option value="0" selected>(please select:)</option>
<option value="1">Send to my inbox</option>
<option value="2">Then delete</option>
<option value="other">Forward to:</option>
</select>
<noscript>
<input type="text" name="choicetext">
</noscript>

<script type="text/javascript" language="JavaScript"><!--
disa = ' disabled';
if(last_choice(document.demoform.menu)) disa = '';
document.write('<input type="text" name="choicetext"'+disa+
' onfocus="check_choice()">');
if(disa && document.styleSheets)
document.demoform.choicetext.style.visibility = 'hidden';
//--></script>

<p>
<input type="submit">
</form>

glenngv
11-14-2002, 10:35 AM
if an option needs a field, set its value to "other" like this:

<select name="menu" onchange="process_choice(this,document.demoform.choicetext)">
<option value="0" selected>(please select)</option>
<option value="1">Send to my inbox</option>
<option value="2">Then delete</option>
<option value="other">Other1 (This needs a field)</option>
<option value="other">Other2 (This needs a field)</option>
<option value="other">Other3 (This needs a field)</option>
</select>

then change the last_choice() function like this:

function last_choice(selection) {
return selection.options[selection.selectedIndex].value=="other";
}


you may want to rename the last_choice() function to an appropriate one.

Try_Try_Again
11-14-2002, 07:56 PM
Thank you, Glenn! that worked.