JavaScript autopopulate a text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jambyl
    New Member
    • Oct 2007
    • 8

    JavaScript autopopulate a text box

    I have a JSP page which has two text boxes.
    I would like to be able to enter a number into the first textbox, and to have that number automatically appear in the second textbox multiplied by 3.
    So if I enter number 1 into the first text box, then I want number 3 appear in the second text box.
    Does anyone know how to do that? Do I need to use JavaScript for that?

    Thank you!
  • Ferris
    New Member
    • Oct 2007
    • 101

    #2
    hi

    yes,you do need javascript...

    here's the code I write for you:


    [HTML]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitl ed Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <script language="javas cript">
    function mul()
    {
    var a = document.getEle mentById("first ").value;
    document.getEle mentById("secon d").setAttribut e("value",a * 3);
    }
    </script>
    <body>
    <input type="text" id="first" onChange="mul() ;" value="0" />
    <input type="text" id="second" value="0" />
    </body>
    </html>


    [/HTML]

    Comment

    • jambyl
      New Member
      • Oct 2007
      • 8

      #3
      Ferris,
      Thank you for such a quick reply!!! You code works good, but right now after typing a number in my first text box - I have to click somewhere or hit TAB, Only then the second text box is multiplied by three.
      Is there a way to have the number in second text box change without me leaving the first box?

      Kind regards.

      Comment

      • Ferris
        New Member
        • Oct 2007
        • 101

        #4
        hi

        change line 16 :
        <input type="text" id="first" onChange="mul() ;" value="0" />

        into

        <input type="text" id="first" onKeyUp="mul(); " value="0" />

        Comment

        • jambyl
          New Member
          • Oct 2007
          • 8

          #5
          Ferris,
          This works great!!!

          Thank you!!!

          Comment

          • helimeef
            New Member
            • Sep 2007
            • 77

            #6
            Originally posted by Ferris
            hi

            change line 16 :
            <input type="text" id="first" onChange="mul() ;" value="0" />

            into

            <input type="text" id="first" onKeyUp="mul(); " value="0" />
            Don't do that Ferris! IE doesn't support key events, use a setInterval every few seconds to fill er up.

            Comment

            • Ferris
              New Member
              • Oct 2007
              • 101

              #7
              Originally posted by helimeef
              Don't do that Ferris! IE doesn't support key events, use a setInterval every few seconds to fill er up.

              thanks for telling me that,but I have tested my code in both IE and Firefox,they works well.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by helimeef
                Don't do that Ferris! IE doesn't support key events, use a setInterval every few seconds to fill er up.
                What makes you say that? Can you give an example that doesn't work?

                Comment

                • helimeef
                  New Member
                  • Sep 2007
                  • 77

                  #9
                  Originally posted by acoder
                  What makes you say that? Can you give an example that doesn't work?
                  Sorry I guess it does. I think it's IE6 that doesn't support. Maybe just starting to have nightmares about IE :p

                  Comment

                  Working...