Adding to fields with onChange

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Quinn

    #1

    Adding to fields with onChange

    HI!

    Have two from text fields named "odds" and "stake" where I enter numeric
    values.
    I want to have a text label named "returns" that is automatically updated
    whenever any of these changes.

    Here's my first try (I have cut this out from the form, named form_bet):

    <input name="odds" type="text" id="odds" onChange="updat eReturns();"
    value="0">
    <input name="stake" type="text" id="stake" onChange="updat eReturns();"
    value="0">
    <label id="returns">0 </label>

    This is my function (inside a script tag in HEAD)

    function updateReturns() {
    var returned;
    returned = document.form_b et.odds.value * document.form_b et.stake.value;
    document.form_b et.returns.valu e = returned;
    }

    Doesn't seem to work. Can anyone help?

    Quinn


  • Dennis Marks

    #2
    Re: Adding to fields with onChange

    In article <VyLrc.3214$RL3 .69260@news2.e. nsc.no>, Quinn <stian@mk.no>
    wrote:
    [color=blue]
    > HI!
    >
    > Have two from text fields named "odds" and "stake" where I enter numeric
    > values.
    > I want to have a text label named "returns" that is automatically updated
    > whenever any of these changes.
    >
    > Here's my first try (I have cut this out from the form, named form_bet):
    >
    > <input name="odds" type="text" id="odds" onChange="updat eReturns();"
    > value="0">
    > <input name="stake" type="text" id="stake" onChange="updat eReturns();"
    > value="0">
    > <label id="returns">0 </label>
    >
    > This is my function (inside a script tag in HEAD)
    >
    > function updateReturns() {
    > var returned;
    > returned = document.form_b et.odds.value * document.form_b et.stake.value;
    > document.form_b et.returns.valu e = returned;
    > }
    >
    > Doesn't seem to work. Can anyone help?
    >
    > Quinn
    >
    >[/color]
    Try the following

    <html >
    <head>
    <title>Untitled </title>
    <script type="text/javascript">
    function updatereturns(f orm) {
    form.returns.va lue = form.odds.value * form.stake.valu e;
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="odds" onchange="updat ereturns(this.f orm);"
    value="0">
    <input type="text" name="stake" onchange="updat ereturns(this.f orm);"
    value="0">
    <input type="text" name="returns" readonly="reado nly" value="0">
    </form>
    </body>
    </html>

    --
    Dennis Marks

    To reply change none to dcsi.


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

    Comment

    • Bart Van der Donck

      #3
      Re: Adding to fields with onChange

      The code below should help you. Some remarks:
      - innerText is not for e.g. NS4, but you already use <label>
      - why not onKeyPress in stead of onChange? Updates your <label>
      immediately
      - you could add an onKeyPress event so that user cannot type
      non-numeric characters in the fields and thus avoiding non-numeric
      results inside your <label>. See here how this could be done:


      hope this helps you further
      Bart


      <html>
      <head>
      <script language="javas cript">
      function updateReturns() {
      var returned;
      returned = parseFloat(docu ment.form_bet.o dds.value)
      * parseFloat(docu ment.form_bet.s take.value);
      returns.innerTe xt = returned
      }
      </script>
      </head>
      <body>
      <form name="form_bet" >
      <input name="odds" type="text" id="odds" onChange="updat eReturns();"
      value="0">
      <input name="stake" type="text" id="stake" onChange="updat eReturns();"
      value="0">
      <label id="returns">0 </label>
      </form>
      </body>
      </html>

      Comment

      Working...