www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43

Thread: Add number when not in fucos... how?

  1. #1
    Join Date
    Feb 2004
    Posts
    1,345

    Add number when not in fucos... how?

    I have one text field for amount that will add the amount that will be added in it to an hidden field. I'd like to know how do I make it that way that once the focus is not on the field any more it'll add the amount?

    Thanks!

  2. #2
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Shalom Weee, Mah Shalomcha?

    You got a few options.
    1. Use the onchange or onblur event handler.
    2. Use an interval to continuously recalcuate every half a second or so.
    3. Since you are obviously submitting a form, use the onsubmit event handler.
    Last edited by Ultimater; 11-01-2005 at 05:07 PM.

  3. #3
    Join Date
    Feb 2004
    Posts
    1,345
    I can't do the opposite from onFocus. Meanning, calling a function ONLY when the user LEFT the field?

  4. #4
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    	"http://www.w3.org/TR/html4/strict.dtd">
    <html dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title>Ultimater's Example</title>
    <script type="text/javascript">
    function recalc(t){
    var n1=Number(t.f1.value);
    var n2=Number(t.f2.value);
    n1=isNaN(n1)?0:n1
    n2=isNaN(n2)?0:n2
    t.f3.value=n1+n2
    }
    
    </script>
    </head>
    
    <body>
    <form action="#">
    <p>
    <input name="f1" type="text" value="0" onchange="recalc(this.form)"><br>
    <input name="f2" type="text" value="0" onchange="recalc(this.form)">
    <hr>
    <p>
    Sum of 1 &amp; 2<br>
    <input name="f3" type="text" value="0">
    </form>
    
    </body>
    </html>
    Last edited by Ultimater; 11-01-2005 at 05:17 PM.

  5. #5
    Join Date
    Feb 2004
    Posts
    1,345
    Weird, it's working on FF but not in IE... I don't get it.

  6. #6
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Oh sorry, "recalc" is a bad name try "recalc1".
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    	"http://www.w3.org/TR/html4/strict.dtd">
    <html dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title>Ultimater's Example</title>
    <script type="text/javascript">
    function recalc1(t){
    var n1=Number(t.f1.value);
    var n2=Number(t.f2.value);
    n1=isNaN(n1)?0:n1
    n2=isNaN(n2)?0:n2
    t.f3.value=n1+n2
    }
    
    </script>
    </head>
    
    <body>
    <form action="#">
    <p>
    <input name="f1" type="text" value="0" onchange="recalc1(this.form)"><br>
    <input name="f2" type="text" value="0" onchange="recalc1(this.form)">
    <hr>
    <p>
    Sum of 1 &amp; 2<br>
    <input name="f3" type="text" value="0">
    </form>
    
    </body>
    </html>

  7. #7
    Join Date
    Feb 2004
    Posts
    1,345
    Actually, I'm adding the total to something like this:
    $<span id="total">200</span>

    Should it be like that:
    parseFloat( document.getElementById('total').innerHTML ) += n1

    Does it makes sense?

  8. #8
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Did you give this a try?
    Code:
    var e=document.getElementById('total');
    if(!e)return true;//exit the function if the element is not found
    
    if("innerHTML" in e)
    e.innerHTML=parseFloat(e.innerHTML) + n1;
    else if("firstChild" in e && "data" in e.firstChild)
    e.firstChild.data=parseFloat(e.firstChild.data) + n1;
    The only problem with this logic is that if the user keeps on changing the value of the input field and the total price will keep on adding and adding and never re-starting.

    You'd best you a variable to keep track of the total.
    Last edited by Ultimater; 11-01-2005 at 05:58 PM.

  9. #9
    Join Date
    Feb 2004
    Posts
    1,345
    Ani Mishtagea...

    Won't work (IE). The weird thing is that I'm not geting an alert window for the JS error.

  10. #10
    Join Date
    Feb 2004
    Posts
    1,345
    wrong post!
    Last edited by weee; 11-01-2005 at 06:01 PM.

  11. #11
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Can I see what you are trying?
    edit:
    alright, I see the code.

  12. #12
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    document.checkout.bstate doesn't refer to:
    Code:
    <select name="state" onChange="stateCheck('8.25')">

  13. #13
    Join Date
    Feb 2004
    Posts
    1,345
    Here it is:

    <!--
    function recalc (t) {
    var n1=Number(t.f1.value);
    n1=isNaN(n1)?0:n1

    document.getElementById('total').innerHTML=parseFloat( document.getElementById('total').innerHTML ) + n1;
    }
    //-->

    <strong>Additional Costs</strong>
    <input type="text" name="f1" class="qty" onChange="recalc(this.form)">

  14. #14
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Again, for IE change "recalc" to "recalc1" and make sure that is within a form.

    And again:
    The only problem with this logic is that if the user keeps on changing the value of the input field and the total price will keep on adding and adding and never re-starting.

    You'd best you a variable to keep track of the total.

  15. #15
    Join Date
    Feb 2004
    Posts
    1,345
    I changed, now I do get an error.

    Ata rotze kishor ladaf?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles