[thelist] JavaScript value of text in an option element

Tom Dell'Aringa pixelmech at yahoo.com
Mon Jan 19 12:51:08 CST 2004


--- fstorr <fffrancis at fstorr.demon.co.uk> wrote:
> Using JavaScript I need to validate against the contents of the
> text in an <option> element (as opposed to the contents of the
value
> attribute), and cannot work out how to do it. 

Francis, you need to step into the options array using the
selectedIndex property. Then .text is a valid property:

<script>
function foo()
{
var mySel = document.forms['foo'].elements['foo2'];
var textValue = mySel[mySel.selectedIndex].text
alert(textValue)
}
</script>
</head>
<body onload="foo()">
<form name="foo">
<select name="foo2">
<option value="hi">Hello</option>
<option value="hi2">Hello Again</option>
</select>
</form>

If you wanted something other than the selected value, you have to
specify its index like

var textValue = mySel[1].text

HTH

Tom

=====
http://www.pixelmech.com/ :: Web Development Services
http://www.DMXzone.com/ :: JavaScript Author / Every Friday!
http://www.thywordistruth.net/ :: Eternal Life

[Those who say that I am finished, and am through, will have to run over my dead body to beat me...]


More information about the thelist mailing list