0
<!DOCTYPE html>
<html>
<head>

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title> calculator! </title>

 <link rel="stylesheet" href="bootstrap.min.css" />
 <link rel="stylesheet" href="style.css" />


</head>

<body>

first number : <input type="text" name="num1" />
<br><br>
second number : <input type="text" name="num2" />
<br><br>

sum : <input type="text" name="answersum" />
<br><br>

<input type="button" name="add" value ="add" onClick="addnum()" />


<script>
(
function addnum(){

    var num1 = number(document.getElementByName("num1").value);
    var num2 = number(document.getElementByName("num2").value);
    var sum = num1 + num2;

    document.getElementByName("answersum").value = sum;

}


</script> 

</body>
</html>

i would like to add two numbers using JavaScript by assigning each number to a variables. but when i click add nothings happens or is there any ways that i can add this two numbers without using a variables? thank you

3
  • getElementsByName is plural - and Number starts with a capital letter so Number(document.getElementsByName("num1")[0].value) and you have a weird bracket starting your script ( function Commented May 10, 2016 at 11:20
  • Just an aside. The "answersum" element is just what the <output> element was invented for. Commented May 10, 2016 at 11:25
  • noted sir thank you :) Commented May 10, 2016 at 12:05

3 Answers 3

5

N of Number (you wrote number) should be in caps and getElementsByName is plural and returns a collection

function addnum(){    
    var num1 = Number(document.getElementsByName("num1")[0].value); 
    var num2 = Number(document.getElementsByName("num2")[0].value);
    var sum = num1 + num2;
    document.getElementsByName("answersum")[0].value = sum;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Upvoted, though clearly you need more jQuery.
@JamesThorpe I though OP hasn't tagged question with jquery so I didn't suggested jquery based solution.
getElementsByName is plural...
I wouldn't post a jQuery solution even if it was tagged as such - it's a long running meme on SO... go look at the link...!
@MarcosPérezGude I realized it as well. Thanks though
|
2

parseInt(string, radix);

Parameters

string The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.

radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

Comments

-1

function addNumbers()
                {
                        var val1 = parseInt(document.getElementById("value1").value);
                        var val2 = parseInt(document.getElementById("value2").value);
                        var ansD = document.getElementById("answer");
                        ansD.value = val1 + val2;
                }
first number :
<input type="text" name="num1" id="value1" />
<br>
<br>second number :
<input type="text" name="num2" id="value2" />
<br>
<br>sum :
<input type="text" name="answersum" id="answer"/>
<br>
<br>

<input type="button" name="add" value="add" onclick="addNumbers()" />

3 Comments

getElementsByName, and parseInt takes an (optional) index but it's good practice to include it.
man works for me. ur a blessing to me :) hehe thank you soo much
@expert123..please mark as accepted, if its really work for u.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.