By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,900 Members | 1,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,900 IT Pros & Developers. It's quick & easy.

display option in a textbox

P: n/a
//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 */
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<ki*********@yahoo.com> wrote in message
news:39**************************@posting.google.c om...
//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.
Jul 23 '05 #2

P: n/a
McKirahan wrote:
<ki*********@yahoo.com> wrote in message
news:39**************************@posting.google.c om...
<--snip-->
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.
Jul 23 '05 #3

P: n/a
thanx a lot Rob. it works great :)
Jul 23 '05 #4

P: n/a
ki*********@yahoo.com wrote:
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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.