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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change the selected item of a combobox? 1

Status
Not open for further replies.

Kendel

Programmer
Joined
Apr 24, 2002
Messages
1,512
Location
US
Hi All,

Here is what I'm trying to do:

<select name="myCombo" id="myCombo">
<option value="1">One</option>
<option value="2">Two</option>
</select>

<input type="text" name="myTextBox" onBlur="ChangeValue()">

If User enters 2 and tab away, the selected item in the combobox will change to "Two" and vice versa....

Many Thanks.
 
Hello Kendel,

How about making the onblur handler also the handler of select onchange event.
Code:
<html>
<head>
<script language="javascript">
function ChangeValue() {
	var elem=document.getElementById("myCombo");
	document.forms("testform").elements("myTextBox").value=elem.options[elem.selectedIndex].value;
}
</script>
</head>
<body>
<form name="testform">
<select name="myCombo" id="myCombo" onchange="ChangeValue()">
   <option value="1">One</option>
   <option value="2">Two</option>
</select> 
<input type="text" name="myTextBox" onBlur="ChangeValue()">
</form>
</body>
</html>
regards - tsuji
 
Thanks tsuji but it doesn't work. No materwhat I enter in the textbox, it combo always select One & change the value in the textbox to "1".
 
Kendel,

Is that not what you want? so that people cannot enter whatever crabs there? Maybe I misread what you need. (I'm off, until later...)

- tsuji
 
Sorry, that's not what I want. I want the value of dropdown change to One if 1 is entered in the textbox and change to Two if 2 is entered.

Thanks.
 

Is this what you want.


<html>
<head>
<script language="javascript">
function ChangeValue()
{
var elem=document.getElementById("myCombo");
var textval = document.forms("testform").elements("myTextBox").value
for(var i=0; i<document.testform.myCombo.length; i++)
{
if(elem.options.value==textval)
{
elem.options.selected = true;
}
}

}
</script>
</head>
<body>
<form name="testform">
<select name="myCombo" id="myCombo" onchange="ChangeValue()">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<input type="text" name="myTextBox" onBlur="ChangeValue()">
</form>
</body>
</html>
 
Yes, that's what I want. Thanks 4345487!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top