Solved

Fill out a text box by selecting an item from drop down menu

Posted on 2008-11-03
7
351 Views
Last Modified: 2013-11-11
I want to select an item from  drop down menu to fill a text box.
If user doesn't find an appropriate selection from drop down menu then I want user to enter free text in the text box.
Please send me the solution or help me to fix this problem

<HTML>
<HEAD>
<TITLE></TITLE>
<script>
var select2Options = new Array(
            new Array(/* options for Fred*/
                  new Array(/*text*/'bam bam', /*value*/'1.asp'),
                  new Array('zzz', '2.asp')
                  ),
            new Array(/* options for Barney*/
                  new Array('vvv', '3.asp'),
                  new Array('bbb', '4.asp')
                  )
);
function updateSelection()
{
      select1Obj = document.getElementById("s1");      
      select2Obj = document.getElementById("s2");      
      selectedIndex = select1Obj.selectedIndex;
      optionsArray = select2Options[selectedIndex];
 
      for (i = 0; i < optionsArray.length; i++)
      {
            optionText = optionsArray[i][0];
            optionValue = optionsArray[i][1];
            select2Obj.options[i] = new Option(optionText, optionValue);
      }      
}
</script>
</HEAD>
<BODY>
<form id="f1" name="f1" >
      <select name="s1" id="s1" onChange="updateSelection()"> 
            <option name="s1" id="s1" value="Fred">Fred</option>
            <option name="s1" id="s1" value="Barney">Barney</option>
			<option name="s1" id="s1" value="Fred">Jeff</option>
            <option name="s1" id="s1" value="Barney">Docker</option>
			
	  <option name="s1" id="s1" value="Fred">I don't find the item in the lsit</option>
	  
      </select>
           <input type="text" size="20" name="s2" id="s2"> 
      
	   <input type="submit" value="please submit form">
</form>
</BODY>
</HTML>

Open in new window

0
Comment
Question by:sfareed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:fourice
ID: 22867173
I'm not sure what you want to do with:
      for (i = 0; i < optionsArray.length; i++)
      {
            optionText = optionsArray[i][0];
            optionValue = optionsArray[i][1];
            select2Obj.options[i] = new Option(optionText, optionValue);
      }
But try this:

<HTML>
<HEAD>
<TITLE></TITLE>
<script>
var select2Options = new Array(
            new Array(/* options for Fred*/
                  new Array(/*text*/'bam bam', /*value*/'1.asp'),
                  new Array('zzz', '2.asp')
                  ),
            new Array(/* options for Barney*/
                  new Array('vvv', '3.asp'),
                  new Array('bbb', '4.asp')
                  )
);
function updateSelection()
{
      select1Obj = document.getElementById("s1");
      selectedIndex = select1Obj.selectedIndex;
      aValue = select1Obj.options[selectedIndex].value
 
      if(aValue=="manual")
      {
      	document.getElementById('textBoxDiv').style.visibility='';
      }
      else
      {
      	document.getElementById('s2').value="";
      	document.getElementById('textBoxDiv').style.visibility='hidden';
      }
}
</script>
</HEAD>
<BODY>
<form id="f1" name="f1">
	<select name="s1" id="s1" onChange="updateSelection()">
		<option value="Fred">Fred</option>
		<option value="Barney">Barney</option>
		<option value="Jeff">Jeff</option>
		<option value="Docker">Docker</option>
		<option value="manual">I don't find the item in the list</option>
	</select>
	<div id="textBoxDiv" style="visibility:hidden">
		<input type="text" size="20" name="s2" id="s2">
	</div>
	<input type="submit" value="please submit form">
</form>
</BODY>
</HTML>

Open in new window

0
 
LVL 15

Accepted Solution

by:
Tomarse111 earned 500 total points
ID: 22867174
Try the below:

When a user selects an option it populates a disabled text box with that option's text. If they select the last option then the textbox is enabled and the user can type in their own text. It will then become disabled again if a user selects a another option.

If you want the value of the option swap lines:

document.getElementById('s2').value = sel.options[selIndex].text;

with

document.getElementById('s2').value = sel.options[selIndex].value;
<HTML>
<HEAD>
<TITLE></TITLE>
<script>
function updateSelection(sel)
{
	var selIndex = sel.selectedIndex;
	document.getElementById('s2').value = sel.options[selIndex].text;
	if(sel.selectedIndex==sel.options.length-1){
		document.getElementById('s2').disabled=false;				
	}else{
		document.getElementById('s2').disabled=true;
	}		
}
</script>
</HEAD>
<BODY>
<form id="f1" name="f1" >
      <select name="s1" id="s1" onChange="updateSelection(this)"> 
        	<option value="Fred">Fred</option>
            <option value="Barney">Barney</option>
            <option value="Fred">Jeff</option>
            <option value="Barney">Docker</option>                        
         	<option value="Fred">I don't find the item in the lsit</option>          
      </select>
      <input type="text" size="20" name="s2" id="s2" disabled="disabled">       
      <input type="submit" value="please submit form">
</form>
</BODY>
</HTML>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22868029
why mix access?
function updateSelection(sel) {
  var selIndex = sel.selectedIndex;
  var theForm = sel.form;
  theForm['s2'].value = sel.options[selIndex].text;
  if (sel.selectedIndex==sel.options.length-1) {
    theForm['s2'].disabled = false;
    theForm['s2'].select();
    theForm['s2'].focus();
  }
  else theForm['s2'].disabled = true;
}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 15

Expert Comment

by:Tomarse111
ID: 22868151
How do you mean mix access sorry mplungian?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22874211
If you pass a form object, use form access, if not use DOM, but I find that this construct bothers my eyes:

        var selIndex = sel.selectedIndex;
        document.getElementById('s2').value = sel.options[selIndex].text;

instead of


        var selIndex = sel.selectedIndex;
        sel.form['s2'].value = sel.options[selIndex].text;


0
 
LVL 15

Expert Comment

by:Tomarse111
ID: 22874338
Ahhh i understand now. Sorry for that, should of really re read my answer before posting.

and to be honest I didn't realise you could reference a form element in this way:

sel.form['s2'];

Thank you though, I'll try and stick to the same methods in future :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22876506
No points to anyone else?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question