display option in a textbox

K

kindermaxiz

//I have a form such as this:
echo "<form method=\"post\" action=$php_self>
<BR><INPUT TYPE=\"TEXT\" NAME=\"textbox\" SIZE=\"40\">
<BR> <select name=\"searchlist\">
<option value=\"val1\">$x</option>
<option value=\"val2\">$y</option>
<option value=\"val3\">$z</option>
<option value=\"val4\">$t</option>
</select> <p>
<input type=\"submit\" name=\"submit_the_values\" value=\"$submit\">
</form><p>";

/* I want that the option selected in the "searchlist" option get
displayed in my textbox using javascript, how can I do this? thanx in
advance pat */
 
M

McKirahan

//I have a form such as this:
echo "<form method=\"post\" action=$php_self>
<BR><INPUT TYPE=\"TEXT\" NAME=\"textbox\" SIZE=\"40\">
<BR> <select name=\"searchlist\">
<option value=\"val1\">$x</option>
<option value=\"val2\">$y</option>
<option value=\"val3\">$z</option>
<option value=\"val4\">$t</option>
</select> <p>
<input type=\"submit\" name=\"submit_the_values\" value=\"$submit\">
</form><p>";

/* I want that the option selected in the "searchlist" option get
displayed in my textbox using javascript, how can I do this? thanx in
advance pat */

<html>
<head>
<title>opt2txt.htm</title>
<script type="text/javascript">
function opt2txt(that) {
var what = that.options[that.selectedIndex].value;
document.forms[0].textbox.value = what;
}
</script>
</head>
<body>
<form method="post" action="$php_self">
<br>
<input type="text" name="textbox" size="40">
<br>
<select name="searchlist" onchange="opt2txt(this)">
<option value=""></option>
<option value="val2">$y</option>
<option value="val3">$z</option>
<option value="val4">$t</option>
</select>
<p>
<input type="submit" name="submit_the_values" value="$submit">
</form>
<p>
</body>
</html>

I added <option value=""></option> so "onchange" would detect a choice.
 
R

RobG

McKirahan said:

function opt2txt(that) {
var what = that.options[that.selectedIndex].value;
document.forms[0].textbox.value = what;

The last line refers explicitly to the first form in the document - so
add another form above this one and the script either fails or updates
the wrong field.

The syntax for "textbox" is also not cross-browser (it may work in IE,
but certainly doesn't work in Geko browsers). How about:

that.form.elements['textbox'].value = what;

This fixes the syntax and references the calling form based on the
element that fired the script rather than its position in the DOM
(presuming, of course, that the textbox really is in the same form as
the event that fired the script).

Cheers.
 
R

RobG

thanx a lot Rob. it works great :)

That's OK.

Incidentally, I lied about the syntax for:

that.form.textbox.value = what;

it will work fine in Geko browsers, I got confused with IE
allowing id's to be used as global variables. But in any
case,

that.form.elements['textbox'].value = what;

is the preferred syntax.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top