Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Dynamic Sum as you type (onkeypress?)

    This seems like an issue that has to have come up in the past, but I didn't find anything with a search, so here I am...

    Here's a simplified version of what I'm trying to do
    Code:
    <HTML>
    <HEAD>
    <SCRIPT type="text/javascript">
    function sumFields()
    {
        with( form1 )
        {
            var x;
            x = Number(num1.value) + Number(num2.value);
            total.value = x;
        }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM id=form1>
    <INPUT id="num1" onkeypress="sumFields()">
    <INPUT id="num2" onkeypress="sumFields()">
    
    <INPUT id="total" readonly>
    </FORM>
    
    </BODY>
    
    </HTML>
    Now, the problem with this code is that the onkeypress event occurs before the change is actually made to the field value. So if I go to type in 123 into field one, then 234 into field two, the sum is always 'trailing' by one characters.


    field 1 . . . field 2 . . . total
    1 . . . . . . . . . . . . . 0
    12. . . . . . . . . . . . . 1
    123 . . . . . . . . . . . . 12
    123 . . . . . 2 . . . . . . 123
    123 . . . . . 23. . . . . . 125
    123 . . . . . 234 . . . . . 146


    Is there an easy way to get the behavior I'm after?

    Thanks in advance,
    Darren

  • #2
    New Coder bigtiger's Avatar
    Join Date
    Oct 2005
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did you tryed change onkeypress to onchange ?

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    SCRIPT type="text/javascript">
    function 
    sumFields()
    {
        
    withform1 )
        {
            var 
    x;
            
    Number(num1.value) + Number(num2.value);
            
    total.value x;
        }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM id=form1>
    <INPUT id="num1" onchange="sumFields()">
    <INPUT id="num2" onchange="sumFields()">

    <INPUT id="total" readonly>
    </FORM>

    </BODY>

    </HTML> 
    Hope this help

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I did, but the onchange even occurs after the focus leaves the field. It doesn't update while you type. I was looking for a way to update the sum field as you enter each charcter into the other fields... no leaving the field required.

  • #4
    New Coder bigtiger's Avatar
    Join Date
    Oct 2005
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi, Darren, I do not know if this work for you. But this is a workaround for a low volume usage.

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    SCRIPT type="text/javascript">
    function 
    sumFields()
    {
        
    setTimeout("callback()"10);
    }

    function 
    callback()
    {
        
    with (form1)
        {
            var 
    x;
            
    Number(num1.value) + Number(num2.value);
            
    total.value x;
        }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM id=form1>
    <INPUT id="num1" onkeypress="sumFields()">
    <INPUT id="num2" onkeypress="sumFields()">
    <INPUT id="total" readonly>
    </FORM>

    </BODY>

    </HTML> 
    But I do think should have better solution out there
    Hope this help
    Eric,
    Last edited by bigtiger; 11-02-2006 at 05:30 AM.


  •  

    Posting Permissions

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