Results 1 to 3 of 3

Thread: onkeypress?

  1. #1
    Join Date
    Jan 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onkeypress?

    I have 3 input forms, 1st, 2nd and 3rd number.
    I want to display the total after entering the numbers,
    please help me.
    Last edited by kat32; 02-26-2009 at 01:42 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Not as good as the moderators can do on here, but nonetheless functional, and not using innerhtml. ....

    This Also suppresses "NaN" errors.

    parseFloat() is what you are looking for though. It converts strings to values.

    Code:
    <script type="text/javascript">
    function addstuff()
    {
    function silentErrorHandler() {return true;}
    window.onerror=silentErrorHandler;
    first = parseFloat(document.getElementById('val1').value);
    second = parseFloat(document.getElementById('val2').value);
    third = parseFloat(document.getElementById('val3').value);
    document.getElementById('result').firstChild.nodeValue = first + second + third;
    if (document.getElementById('result').firstChild.nodeValue == 'NaN') 
    {document.getElementById('result').firstChild.nodeValue = "";}
    else
    {document.getElementById('result').firstChild.nodeValue = first + second + third;}
    }
    </script>
    One:<input type="text" id="val1" name="val1" onkeyup="addstuff();"><br>
    Two:<input type="text" id="val2" name="val2" onkeyup="addstuff();"><br>
    Three:<input type="text" id="val3" name="val3" onkeyup="addstuff();"><br>
    <a id="result" name="result">&nbsp;</a>
    Last edited by Falkon303; 02-26-2009 at 01:56 AM.
    document.write is document.wrong

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •