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!

Get Text Value of a Drop Down

Status
Not open for further replies.

ter79

IS-IT--Management
Jul 11, 2001
106
US
I have a drop down box that contains the option value and then the text for the value.

I have the code to select the value of the drop box but what I need is to be able to get the text that corresponds to the value

Here is the code that I'm using to the get the value

var ofc = document.form.ofclist.value;

I also have two other drop down boxes on the form, if that makes any difference on the code..

Hopefully I'm making sense of what I'm trying to do
Thanks in advance
 
From Getting all values in select (thread216-674207):

(put this in a new .html and try it)
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>

It uses the DOM standard, so it works in all browsers (v 4-5 +), I tested it on IE6 and Firebird 0.7

You could also use something like .innerHTML or .innerText for a browser-specific solution.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
much easier

var textValue = document.form.ofclist.text;
 
ter79,

Use this...

var ddl = new Object();
ddl = document.form.ofclist;
var ofc = ddl.options[ddl.selectedIndex].text;

If you don't want to create the new object as above, you can do it this way...

var ofc = document.form.ofclist.options[document.form.ofclist.selectedIndex].text;

Either way should work.

Game Over, Man!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top