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!

I have this code which puts the sel

Status
Not open for further replies.

Andy6666uk

Programmer
Jul 11, 2001
17
GB
I have this code which puts the selection from a list box into a textbox. But I have more than one textbox on the page and want the selection to go into whichever textbox has focus. Any help greatly appreciated.

function handleListChange(theList) {
var numSelected = theList.selectedIndex ;

if (numSelected != 0) {
textValue = document.form.textbox.value ;
textValue = textValue + theList.options[numSelected].value ;
document.form.textbox.value = textValue ;
theList.selectedIndex = 0 ;
document.form.textbox.focus() ;
}
}
<form name=form action=&quot;etc.&quot; method=&quot;post&quot;>
<input type=&quot;text&quot; name=&quot;textbox&quot;>
<input type=&quot;text&quot; name=&quot;textbox2&quot;>
<input type=&quot;text&quot; name=&quot;textbox3&quot;>

<select name=&quot;e&quot; size=&quot;1&quot; onChange=&quot;handleListChange(this)&quot;>
<option value=&quot;0&quot;> 0 </option>
<option value=&quot;1&quot;> 1 </option>
<option value=&quot;2&quot;> 2 </option>
</select>
</form>

thanks again.
 
Interesting. I don't see how the text box could have focus if you just selected something from a drop down? Best you could probably do is update a specific text box based on the selection of something from a specific drop down. Maybe you miss stated your intentions. Let us know so we can understand. Thanks

Rachel....
 
Actually, you can't do it like that. As soon as the user selects the drop-down list, the text boxes will lose focus.

You could perhaps use a radio button to specify which textbox it should be in.

Try this:

Code:
<html>
<head>
	<title>The selection list</title>
	<script name=&quot;Javascript&quot;>
	<!--
var txtSelected = &quot;a&quot;;
function handleListChange(theList)
{
  var numSelected = theList.selectedIndex;
  if (numSelected != 0)
  {
  	if (txtSelected == &quot;a&quot;)
    {
	 textValue = document.form.textbox.value ;
     textValue = textValue + theList.options[numSelected].value ;
	 document.form.textbox.value = textValue;
	}
	else if (txtSelected == &quot;b&quot;)
	{
	 textValue = document.form.textbox2.value ;
     textValue = textValue + theList.options[numSelected].value ;
	 document.form.textbox2.value = textValue;
	}
	else if (txtSelected == &quot;c&quot;)
	{
	 textValue = document.form.textbox3.value ;
     textValue = textValue + theList.options[numSelected].value ;
	 document.form.textbox3.value = textValue;
	}
    theList.selectedIndex = 0;
  }
}
-->
</script>
</head>

<body>


<form name=form action=&quot;etc.&quot; method=&quot;post&quot;>
<input type=&quot;radio&quot; name=&quot;choose&quot; onclick=&quot;javascript: txtSelected = 'a'&quot; checked>
<input type=&quot;text&quot; name=&quot;textbox&quot;><br>
<input type=&quot;radio&quot; name=&quot;choose&quot; onclick=&quot;javascript: txtSelected = 'b'&quot;>
<input type=&quot;text&quot; name=&quot;textbox2&quot;><br>
<input type=&quot;radio&quot; name=&quot;choose&quot; onclick=&quot;javascript: txtSelected = 'c'&quot;>
<input type=&quot;text&quot; name=&quot;textbox3&quot;>

<select name=&quot;e&quot; size=&quot;1&quot; onChange=&quot;handleListChange(this)&quot;>
  <option value=&quot;0&quot;> 0 </option>
  <option value=&quot;1&quot;> 1 </option>
  <option value=&quot;2&quot;> 2 </option>
</select>
</form>


</body>
</html>

I can see what your trying to do, but I don't think it's possible. As soon as they go to select a value, the txt boxes lose focus.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top