faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

69 of 79 people (87%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

JavaScript: List: Option: Text: How to get the text of a chosen option in <SELECT><OPTION> list?

Oct 16th, 2004 16:11
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 08 September 2004 - 03:51 am ------------------

JavaScript: List: Option: Text: How to get the text of a chosen option 
in <SELECT><OPTION> list?

---

Use the 'text' property

---

--- cut here: begin --------------------------------------------------

<HTML>

<BODY>

<FORM
  NAME = "form1"
>

<SELECT
  NAME = "list1"
  ONCHANGE = ' alert( document.forms.form1.list1.options[ 
document.forms.form1.list1.selectedIndex ].text );'
>
 <OPTION>Your option 1</OPTION>
 <OPTION>Your option 2</OPTION>
</SELECT>

</FORM>

</BODY>

</HTML>

--- cut here: end ----------------------------------------------------

---
---

Note:

the 'value' property does not give a result here
(it returns an empty string)

e.g.

alert( document.forms.form1.list1.options[ 
document.forms.form1.list1.selectedIndex ].value );

----------------------------------------------------------------------