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 2 of 2

Thread: addition help

  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    addition help

    I have

    Code:
    function dototal()
    {
    tamount=parseInt(document.myform.amount1.value)+parseInt(document.myform.amount2.value)+parseInt(document.myform.amount3.value);
    document.getElementById('total').innerHTML=tamount;
    
    
    }
    the problem is if amount is blank it can't add it -- how can I change it that if it is blank it should treat it as 0?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Like this:-

    Code:
    <script type = "text/javascript">
    
    function dototal() {
    var a = parseInt(document.myform.amount1.value)  || 0;
    var b = parseInt(document.myform.amount2.value)  || 0;
    var c = parseInt(document.myform.amount3.value)  || 0;
    var tmaount = a+b+c;
    document.getElementById('total').innerHTML=tamount;
    
    }
    "To generalize on women is dangerous. To specialize in them is infinitely worse." - Rudolph Valentino


  •  

    Posting Permissions

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