PDA

View Full Version : Select issue


ecnarongi
03-12-2003, 05:03 PM
How do I get the text value out of a select menu?

<form name="form1">
<select name="sel">
<option selected value="3">TEXT</option>
</select>
</form>

document.form1.sel.value = 3 how do I get a value of "TEXT"? all help is appreciated. thanks

brothercake
03-12-2003, 05:19 PM
The most cross-browser solution I know of is this:

document.formname.selectname.options[document.formname.selectname.options.selectedIndex].value;

ecnarongi
03-12-2003, 05:27 PM
will try thanks :thumbsup:

ecnarongi
03-12-2003, 05:42 PM
in IE6 I get a javascript error with that code. here's my little test

<html>
<head>
<!--
function moveme()
{
document.form1.val.value = document.form1.sel.options[document.form1.sel.options.selectedIndex].value
}
-->
</head>
<body>
<form name="form1">
<select name="sel" onChange="moveme()">
<option value="3">TEXT1</option>
<option value="2">TEXT2</option>
</select><br>
<input type="text" name="val">
</form>
</body>
</html>

I looked for stupid mistakes I couldn't find any. Can you help me out?

brothercake
03-12-2003, 07:46 PM
javascript has to be in a <script> element:

<script type="text/javascript">
<!--
function moveme()
{
document.form1.val.value = document.form1.sel.options[document.form1.sel.options.selectedIndex].value
}
//-->
</script>

cheesebagpipe
03-12-2003, 11:06 PM
That'll get you the .value property - not the displayed text:

document.form1.sel.options[document.form1.sel.options.selectedIndex].text

This is really the long way around; if you're calling the function from the Select.onchange handler, and only need the text of the selected option:

<form name="form1">
<select name="sel" onchange="moveme(options[selectedIndex])">

......
function moveme(selectedText) {
.......

ecnarongi
03-13-2003, 03:18 PM
WOW I can't believe I missed that. My problem was the <script> tags. This corp. ish must be getting to me. I think I need a break. Thanks guys