0

To learn and study JavaScript, im trying to do a calculator, and as start i tried to do a sum operation by taking 2 input. But somehow there is a problem that i cant see. Can you help me?

Here is the code:

<html> 
    <body>
        <h1> JavaScript Test </h1>
        Sum The Nums Up!
        <br>
        <form>
            Number 1: <input type="text" name="n1" /><br>
            Number 2: <input type="text" name="n2" /><br>
        </form>
        <button id="b" onclick="func();" />Sum</button>
        <p id="b"></p>
        <script type="text/javascript">
            function sum() {
                var nn1 = document.getElementById("n1").value;
                var nn2 = document.getElementById("n2").value;
                var sum = parseInt(nn1) + parseInt(nn2);
                document.write(sum);
            }
        </script>
    </body>
</html>
5
  • change onclick="func();" to onclick="sum();" Commented Jun 23, 2014 at 6:52
  • 1
    And if you want to keep the "form" you have and display the results in #b, change document.write(sum); to document.getElementById('b').innerHTML = sum; Commented Jun 23, 2014 at 6:54
  • <input type='text' name='n1'> + document.getElementById('n1') won't fly: the input elements don't have an id property, only a name attribute. document.write is evil, BTW... Commented Jun 23, 2014 at 7:09
  • @EliasVanOotegem: Not as evil as evil() ;-) Commented Jun 23, 2014 at 7:12
  • @Cerbrus: Of course, that would be evil, instead of merely evil :-) Commented Jun 23, 2014 at 7:19

3 Answers 3

1

Replace:

function sum() {

With:

function func() {

This prevents you from assigning 2 different types / values to the same variable name.
(You've got an sum function and a sum variable in your question.)


Replace:

document.write(sum);

With

document.getElementById('b').innerHTML = sum;

This snippet adds the result of sum to <p id="b"></p> (So, the result may be: <p id="b">18</p>, for example), instead of arbitrarily adding it to the end of your HTML.


And finally, replace:

Number 1: <input type="text" name="n1"/><br>
Number 2: <input type="text" name="n2"/><br>

With:

Number 1: <input type="text" id="n1"/><br>
Number 2: <input type="text" id="n2"/><br>

document.getElementById looks for the id attribute in your HTML, not for the name.

Sign up to request clarification or add additional context in comments.

3 Comments

Would the sum naming really matter ? I think sum() from onclick="sum();" would point to the function sum() with no problem and .write(sum); woul point wihtout problem to the var sum
Yea, I know the var sum is scoped to the function, but it's bad form to re-use names that way. Forget the var just once, and your function no longer exists after running it. It works, but it's "risky". That's why I suggested that change.
Hmmm true, use strict also helps in such cases :)
1

There are two problems:-

  1. Change onclick="func();" to onclick="sum();" , you are calling the wrong/undefined function.
<button id="b" onclick="sum();"/>Sum</button>
  1. Assign input tags id as n1 and n2, you have assigned them as name and are referring to them based on id getElementById()
Number 1: <input type="text" id="n1"/><br>
Number 2: <input type="text" id="n2"/><br>

Comments

-1
        <html>
        <body>
        <h1> JavaScript Test </h1>
        Sum The Nums Up!
        <br>
        <form>
        Number 1: <input type="text" id="n1" name="n1"/><br>
        Number 2: <input type="text" id="n2" name="n2"/><br>
        </form>
        <button id="b" onclick="sum();"/>Sum</button>
        <p id="b"></p>
        <script type="text/javascript">
        function sum()
        {
            var nn1 = document.getElementById("n1").value;
            var nn2 = document.getElementById("n2").value;
            var sum = parseInt(nn1) + parseInt(nn2);
            document.write(sum);
            }
        </script>
        </body>
        </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.