this.SelectedIndex does not work - why...?

S

swissreporter

Hi - I have a really stupid failure of code, can't figure it out.
Two fields in a form, Textinput and select; if text is written into
input, select changes "selected" to a matching option. This works.
Now I would like to put the WHOLE value of the selected option into
the input field, if the user klicks on something in the select-field.
This DOESN'T work, because I seem not to reference the selected item
correctly.
But then again i did everything according to the various tutorials.
THX for a quick hint.

Code:

___________________
<html>
<head>
<title>Some page</title>
<script language="JavaScript">
<!--Javascript:
function selectOption(oTxt){
var sel = oTxt.form.select;
var len = sel.options.length;
sel.selectedIndex = len - 1; //select 'Select One' option by default
for (var i=0; i<len; i++){
if (sel.options.value.indexOf(oTxt.value)!=-1){
sel.selectedIndex = i;
break;
}
}
}
//--> </script>

<script language="JavaScript">
<!--Javascript:
function ChangeField (oTxt){
var stamm = oTxt.form.write;
var sel=oTxt.SelectedIndex;
stamm.value=oTxt.options[sel].value;

}
//--> </script>

</head>

<body>
<form name="Anmeldung" method="post" action="anmelden.php">
<input type="text" name="write" size="23"
onkeyup="selectOption(this)" onblur="selectOption(this)" value="">

<select name="select" size="3" onclick="ChangeField(this)">
<option value="A name">A name</option>
<option value="B Name">B name</option>
<option value="C name">C name</option>
<option value="D name">D name</option>
<option value="E name">E name</option>
</select>
</form>
</body>
</html>
___________________
 
F

Fred

Hi - I have a really stupid failure of code, can't figure it out.
Two fields in a form, Textinput and select; if text is written into
input, select changes "selected" to a matching option. This works.
Now I would like to put the WHOLE value of the selected option into
the input field, if the user klicks on something in the select-field.
This DOESN'T work, because I seem not to reference the selected item
correctly.
But then again i did everything according to the various tutorials.
THX for a quick hint.

Code:

___________________
<html>
<head>
<title>Some page</title>
<script language="JavaScript">

The language attribute is depricated, type is required.

<!--Javascript:

Forget that silliness.

function selectOption(oTxt){
var sel = oTxt.form.select;
var len = sel.options.length;
sel.selectedIndex = len - 1; //select 'Select One' option by default
for (var i=0; i<len; i++){
if (sel.options.value.indexOf(oTxt.value)!=-1){
sel.selectedIndex = i;
break;}


Or just return. Indenting of code using 2 spaces is preferred for
posted code, with manual wrapping at about 70 characters.

}
}

//--> </script>

<script language="JavaScript">
<!--Javascript:
function ChangeField (oTxt){
var stamm = oTxt.form.write;
var sel=oTxt.SelectedIndex;

Javascript is case sensitive, use:

var sel=oTxt.selectedIndex;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top