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
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How take an input and add it

    How can I ask the user for two different numbers and add them?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    This is a pretty basic homework question which you should really answer using a Javascript textbook. But

    Code:
    First Number <input type = "text" id = "first"><br>
    Second Number <input type = "text" id = "second"><br>
    <input type = "button" value = "Add 'em up" onclick = "add()"><br>
    Total  <input type = text" id = "total" readonly>
    
    
    <script type = "text/javascript">
    function add() {
    var a = document.getElementById("first").value;
    var b = document.getElementById("second").value;
    var t = Number(a) + Number (b);  // convert from strings to numbers
    document.getElementById("total").value = t;
    }
    
    </script>

    “He who knows not and knows not he knows not: he is a fool - shun him.
    He who knows not and knows he knows not: he is simple - teach him.
    He who knows and knows not he knows: he is asleep - wake him.
    He who knows and knows he knows: he is wise - follow him.”

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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