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

Get <Select> Display Value 2

Status
Not open for further replies.

maxhugen

Programmer
May 25, 2004
498
AU
I have a SELECT, where the Value is `category_id`, and the `category_name` is displayed.

Can anyone pls suggest how I can get the 'display' value for the selected option, ie the `category_name`, in javascript?

MTIA!

Max Hugen
Australia
 
So your code looks something like this?
Code:
...
<select id="category_name">
  <option value="">None</option>
  <option value="book_1382">Books</option>
  <option value="zebra_82763">Zebras</option>
</select>
...
You can access the value attribute of the selected option using this kind of javascript:
Code:
...
var selectElement = document.getElementById('category_name');
var selectedValue = selectElement[selectElement.selectedIndex].value;
alret (selectedValue);
...
You could gain access to the select element in many ways (I have used an id for simplicity). You could also trigger this by running a function when the select element is changed (when it fires the onchange event).

Let us know how you go!

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
@BabyJeffy, thanks for your reply! I wasn't even sure how to reference the value property of a select, so that's a great help!

Dan, thanks for pointing out the properties... I see now, that what I need is the 'text' property. Now it's working for me:

Code:
var el = document.getElementById("category_id");
var val = el[el.selectedIndex].text;
document.getElementById("category").value = val;

Appreciate the advice!



Max Hugen
Australia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top