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

Getting all values in select 2

Status
Not open for further replies.

kristolcrawley

Programmer
Jun 17, 2002
19
US
I have a select that has id - name. If the user sets one of the options, I'd like to set a different form field to the name value. What I'm getting is the id. The options are built from a table and they change all the time so I can't hardcode the values to search through. I need the id to be set as well so I can't change that. This is what I have that's not working.

function displayClient() {
document.myform.client.value = document.myform.clientname.value;
}


<select class=&quot;select&quot; name=&quot;clientid&quot; onChange=&quot;displayClient()&quot; >


<option value=&quot;&quot; SELECTED>


<option value=&quot;5&quot;>AOL

<option value=&quot;7&quot;>AT&amp;T

<option value=&quot;4&quot;>Amdahl

<option value=&quot;18&quot;>Associates

<option value=&quot;9&quot;>B of A Change it

<option value=&quot;8&quot;>Bank of America

<option value=&quot;10&quot;>Bellsouth

<option value=&quot;12&quot;>Chase

<option value=&quot;13&quot;>Cisco

<option value=&quot;15&quot;>Citibank

<option value=&quot;16&quot;>Citicorp

<option value=&quot;19&quot;>Citigroup

<option value=&quot;20&quot;>Delta

<option value=&quot;21&quot;>Delta Technologies

<option value=&quot;24&quot;>EDS

<option value=&quot;1&quot;>Eastman Kodak

<option value=&quot;23&quot;>Electroic Data Systems

<option value=&quot;25&quot;>Etrade

<option value=&quot;26&quot;>Excite

<option value=&quot;27&quot;>Federal Express

<option value=&quot;28&quot;>Fedex

<option value=&quot;29&quot;>Ford

<option value=&quot;32&quot;>GM

<option value=&quot;30&quot;>GM/EDS

<option value=&quot;57&quot;>General Electric

<option value=&quot;31&quot;>General Motors

<option value=&quot;33&quot;>Hotmail

<option value=&quot;34&quot;>IBM

<option value=&quot;11&quot;>J P Morgan

<option value=&quot;3&quot;>Kodak

<option value=&quot;2&quot;>Kodak Canada

<option value=&quot;35&quot;>Level3

<option value=&quot;36&quot;>Lucent

<option value=&quot;50&quot;>MCI

<option value=&quot;37&quot;>Merrill Lynch

<option value=&quot;38&quot;>Moto

<option value=&quot;39&quot;>Motorola

<option value=&quot;52&quot;>Nokia

<option value=&quot;41&quot;>Nortel

<option value=&quot;40&quot;>Nortel Networks

<option value=&quot;42&quot;>Oracle

<option value=&quot;43&quot;>Qwest

<option value=&quot;48&quot;>Skytel

<option value=&quot;14&quot;>Solomon Smith Barney

<option value=&quot;44&quot;>Sprint

<option value=&quot;45&quot;>Texas Instruments

<option value=&quot;17&quot;>Travelers

<option value=&quot;49&quot;>Uunet

<option value=&quot;46&quot;>Verizon

<option value=&quot;47&quot;>Worldcom

<option value=&quot;51&quot;>Xerox
<!-- END FOREACH ON Clientnam DROPLET -->
</SELECT>

</TD>
<TD><B>OR</B></TD>
<TD ALIGN=&quot;LEFT&quot;><input type=&quot;text&quot; name=&quot;client&quot; size=&quot;20&quot; BEAN=&quot;User.order.Client&quot; ONKEYPRESS=&quot;return rmSpecial(this, event)&quot; class=&quot;select&quot; ONKEYPRESS=&quot;return changeCase(this, event)&quot;>
 
Use start and end tags for the options and refer to

Code:
var clientSelection = document.myform.clientid;
var iSelected = clientSelection.selectedIndex;
var nameSelected = clientSelection.options[iSelected].text;
 
instead of
var nameSelected = clientSelection.options[iSelected].text;
I'd use the DOM:
[tt]curValue = selectObj.options.firstChild.nodeValue;[/tt]

And better yet, build your select using correct syntax - full working example file:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>testbed</title>
<style type=&quot;text/css&quot;>
</style>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function displayClient(selectObj) {
	//get textbox reference
	var textObj = document.getElementById(&quot;textID&quot;);
	
	//get 'selected' option reference
	var selectedOption = selectObj.options[selectObj.selectedIndex];
	
	//assign selected option text (using DOM) to textbox.value
	textObj.value = selectedOption.firstChild.nodeValue;

	//you can also get the value=&quot;&quot; string with selectedOption.value 
}
</script>
</head>
<body>
<form>
<select name=&quot;selectName&quot; id=&quot;selectID&quot; size=&quot;1&quot; onchange=&quot;displayClient(this)&quot;>
	<option value=&quot;NONE&quot; selected>Please select:</option>
	<option value=&quot;2&quot;>Kodak Canada</option>
	<option value=&quot;33&quot;>Hotmail</option>
</select>
<input type=&quot;text&quot; name=&quot;textName&quot; id=&quot;textID&quot; size=&quot;30&quot; />
</form>
</body>
</html>

enjoy

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
stinker, a [ignore][/ignore] snuck through. Here's the test code for a working html example again:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>testbed</title>
<style type=&quot;text/css&quot;>
</style>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function displayClient(selectObj) {
	//get textbox reference
	var textObj = document.getElementById(&quot;textID&quot;);
	
	//get 'selected' option reference
	var selectedOption = selectObj.options[selectObj.selectedIndex];
	
	//assign selected option text (using DOM) to textbox.value
	textObj.value = selectedOption.firstChild.nodeValue;

	//you can also get the value=&quot;&quot; string with selectedOption.value 
}
</script>
</head>
<body>
<form>
<select name=&quot;selectName&quot; id=&quot;selectID&quot; size=&quot;1&quot; onchange=&quot;displayClient(this)&quot;>
	<option value=&quot;NONE&quot; selected>Please select:</option>
	<option value=&quot;2&quot;>Kodak Canada</option>
	<option value=&quot;33&quot;>Hotmail</option>
</select>
<input type=&quot;text&quot; name=&quot;textName&quot; id=&quot;textID&quot; size=&quot;30&quot; />
</form>
</body>
</html>

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thank you Clarkin and rac2.

Clarkin, no worries, I have an initial option if they haven't chosen anything, just didn't show it in the code. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top