display option in a textbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kindermaxiz@yahoo.com

    display option in a textbox

    //I have a form such as this:
    echo "<form method=\"post\" action=$php_sel f>
    <BR><INPUT TYPE=\"TEXT\" NAME=\"textbox\ " SIZE=\"40\">
    <BR> <select name=\"searchli st\">
    <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_t he_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 */
  • McKirahan

    #2
    Re: display option in a textbox

    <kindermaxiz@ya hoo.com> wrote in message
    news:39615087.0 409151242.4a902 342@posting.goo gle.com...[color=blue]
    > //I have a form such as this:
    > echo "<form method=\"post\" action=$php_sel f>
    > <BR><INPUT TYPE=\"TEXT\" NAME=\"textbox\ " SIZE=\"40\">
    > <BR> <select name=\"searchli st\">
    > <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_t he_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 */[/color]

    <html>
    <head>
    <title>opt2txt. htm</title>
    <script type="text/javascript">
    function opt2txt(that) {
    var what = that.options[that.selectedIn dex].value;
    document.forms[0].textbox.value = what;
    }
    </script>
    </head>
    <body>
    <form method="post" action="$php_se lf">
    <br>
    <input type="text" name="textbox" size="40">
    <br>
    <select name="searchlis t" onchange="opt2t xt(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_th e_values" value="$submit" >
    </form>
    <p>
    </body>
    </html>

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


    Comment

    • RobG

      #3
      Re: display option in a textbox

      McKirahan wrote:
      [color=blue]
      > <kindermaxiz@ya hoo.com> wrote in message
      > news:39615087.0 409151242.4a902 342@posting.goo gle.com...[/color]

      <--snip-->
      [color=blue]
      > function opt2txt(that) {
      > var what = that.options[that.selectedIn dex].value;
      > document.forms[0].textbox.value = what;[/color]

      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.eleme nts['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.

      Comment

      • kindermaxiz@yahoo.com

        #4
        Re: display option in a textbox

        thanx a lot Rob. it works great :)

        Comment

        • RobG

          #5
          Re: display option in a textbox

          kindermaxiz@yah oo.com wrote:
          [color=blue]
          > thanx a lot Rob. it works great :)[/color]

          That's OK.

          Incidentally, I lied about the syntax for:

          that.form.textb ox.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.eleme nts['textbox'].value = what;

          is the preferred syntax.

          Comment

          Working...