-2

What I want to do is get values from two forms and then add them and display the result, the problem is that it's not working and can't find out why. Is something simple but I'm new in Javascript =/. Here is my code

<!DOCTYPE html>
<html> 
    <head> 
        <title> Random Page </title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form>
            Escriba el primer número: <input type="number" id="txtNumber1">
        </form> 
        <br>
        <form>
            Escriba el segundo número: <input type="number" id="txtNumber2">
        </form> 
        <br>
        <button id="btnSumar" onclick="Sumar()"> ¡Sumar! </button> 
        <br>
        <p id="p"> I'm a paragraph </p>
        <script>
            function Sumar() {
                document.getElementById("p").innerHTML="It works until here.";
                var num1 = document.getElementById("txtNumber1").value;
                var num2 = document.getElementById("txtNumber2").value;
                this.resultado = num1 + num2;
            }
            document.getElementById("p").innerHTML="The result is " + resultado;
        </script>
    </body>
</html>

Any help will be appreciated. Tahnks.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Blackmore
  • 167
  • 1
  • 3
  • 15
  • [See this answer](http://stackoverflow.com/a/20216482/1377002) – Andy Nov 26 '13 at 16:28
  • What do you think `this.resultado = num1 + num2;` is doing? When do you think `document.getElementById("p").innerHTML="The result is " + rst.resultado;` executes? The innerHTML line does not magically update when the variable is updated! – epascarello Nov 26 '13 at 16:30
  • @UDB onclick of the button "btnSumar" – Blackmore Nov 26 '13 at 16:34

3 Answers3

1
<script>
    function Sumar() {
        document.getElementById("p").innerHTML="It works until here.";
        var num1 = document.getElementById("txtNumber1").value;
        var num2 = document.getElementById("txtNumber2").value;
        var resultado = parseInt(num1) + parseInt(num2);
        document.getElementById("p").innerHTML="The result is " + resultado;
    }
</script>
Christos
  • 53,228
  • 8
  • 76
  • 108
0

Can you try this, I found invalid methods this.resultado and rst.resultado in your code since there is no root cause for this, you can use parseInt or parseFloat based on your input number

  function Sumar() {
            document.getElementById("p").innerHTML="It works until here.";
            var num1 = document.getElementById("txtNumber1").value;
            var num2 = document.getElementById("txtNumber2").value;

            var result = num1 + num2; // you can use parseInt or parseFloat based on your input number 
            document.getElementById("p").innerHTML="The result is " +  result;
        }
Krish R
  • 22,583
  • 7
  • 50
  • 59
-1

Here are two examples, one using standard JavaScript, which should answer the basic question, and an example using globalize.js, which will handle international styles of numerical notation.

Using standard JavaScript

Use the parseInt() (or parseFloat()) function to convert the text field argument to a number and remove characters, move the last innerHTML line into the Sumar() function, and change rst.resultado and this.resutado to resultado. jsFiddle example here.

<script>
    function Sumar() {
        var num1 = parseInt(document.getElementById("txtNumber1").value);
        var num2 = parseInt(document.getElementById("txtNumber2").value);
        resultado = num1 + num2;
        document.getElementById("p").innerHTML="The result is " + resultado;
    }
</script>

Using Globalize.js to Allow International Number Formatting

jQuery offers a script developed with Microsoft called globalize.js that formats numbers based on a locale. In this jsFiddle, I used Spain as the locale as an example, but its capable of many more formats and locales. Try using notation like 1.234,56 to check calculations. You can use the toLocaleString() function to format the calculated number back to a localized format, but this currently only works in Chrome and IE. You could use regex for the other browsers, but this is all I had time for. Here is the basic script, but look at the jsFiddle to see the other js required and the changes that needed to be made to the form tags.

<script>
    function Sumar() {
        var num1 = Globalize.parseFloat(document.getElementById("txtNumber1").value);
        var num2 = Globalize.parseFloat(document.getElementById("txtNumber2").value);
        resultado = num1 + num2;
        document.getElementById("p").innerHTML = "The result is " + resultado.toLocaleString("es-ES");
    }
</script>
Space Age Crystal
  • 394
  • 1
  • 2
  • 13