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 to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding a value to a text input while using getElementById

    All i am tryinh to do is add 123 to the value nombre and displaying that result in resultat. Any help would be appreciated

    <body>

    <form action="#" method="post">
    <input type="text" id="nombre">
    <input type="text" id="resultat">
    <input type="button" onclick="calculus()" value="Calculate"/>
    </form>


    <script type ="text/javascript">
    function calculus()
    {
    var addnumber=document.getElementById("nombre");
    var total= addnumber + 123;
    resultat.value= "total";
    }

    </script>

    </body>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    I am unclear whether you want to add the numeric value 123 to the number input by the user, or concatenate the string value 123. I am assuming that you want to add the number, so that if the user enters 2 the result is 125.


    Code:
    <form action="#" method="post">
    <input type="text" id="nombre">
    <input type="text" id="resultat" readonly>
    <input type="button" onclick="calculus()" value="Calculate"/>
    </form> 
    
    
    <script type ="text/javascript">
    function calculus()   {  // prefer to place the opening brace on the same line, not the following line
    var addnumber=Number(document.getElementById("nombre").value);  // form values are strings unless converted to numbers
    var total= addnumber + 123;  // + will add numbers, concatenate strings
    document.getElementById("resultat").value= total;  // you must use document.getElementById()
    } 
    
    </script>

    document.getElementById("resultat").value= "total"; would place the literal word total in the textbox.


    I heard him then, for I had just
    Completed my design
    To keep the Menai Bridge from rust
    By boiling it in wine.
    - Lewis Carroll
    Last edited by Philip M; 07-04-2012 at 09:36 PM. Reason: Typo

    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes i want to add 123 to the users number. If someone putts in 2 well the result will be 2+123= 125

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Ludwig2012 View Post
    Yes i want to add 123 to the users number. If someone putts in 2 well the result will be 2+123= 125
    The code Philip provided does exactly what you want then.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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