0

I tried adding two numbers by getting the values from the text box and calculate then display it using innerhtml but it isn't working. What mistake am I doing in this code?

<body onload="alert('Hey check out my Calculator')">
 <h1 align="center" >  functional calulator </h1>
 <div class="Calculator" align="center">
    <input type="text" name="text1" id="a1">Enter 1st Number <br><br>
    <input type="text" name="text2" id="a2">Enter 2nd Number<br><br>
    <button   onclick="add()" >Add</button>
    <p id="p1"></p>
    
    <script type="text/javascript"> 
        function add(){
            var a = document.getElementById("a1").value;
            var b = document.getElementByID("a2").value;
            var total = a + b;
            document.getElementById("p1").innerHTML = total;
            
            

        }
        </script>

1
  • Elaborate on what " it isn't working" means please. What are you getting? Commented Sep 29, 2016 at 14:05

4 Answers 4

2

you are adding them as a strings so they are being concatenated you should parse them to integers first

var a = parseInt(document.getElementById("a1").value);
var b = parseInt(document.getElementById("a2").value);
var total = a + b;
document.getElementById("p1").innerHTML = total;
Sign up to request clarification or add additional context in comments.

1 Comment

I suggest to add 10 as second argument of parseInt, old broswers can parse "010" as octal
1

a, b are strings. Maybe you want to parse them to ints?

Comments

0

Try This - You have made spell mistake - Its document.getElementById not document.getElementByID

<body onload="alert('Hey check out my Calculator')">
 <h1 align="center" >  functional calulator </h1>
 <div class="Calculator" align="center">
 	<input type="text" name="text1" id="a1">Enter 1st Number <br><br>
 	<input type="text" name="text2" id="a2">Enter 2nd Number<br><br>
 	<button   onclick="add()" >Add</button>
 	<p id="p1"></p>
 	
	<script type="text/javascript">	
		function add(){
           var a = document.getElementById("a1").value;
			var b = document.getElementById("a2").value;
            
			var total = parseInt(a) + parseInt(b);
			document.getElementById("p1").innerHTML = total;
			
			

		}
		</script>

Comments

0

getElementByID should be getElementById in var b = document.getElementById("a2").value;

and do parseInt() for your a and b to consider them as a number.

<body onload="alert('Hey check out my Calculator')">
  <h1 align="center">  functional calulator </h1>
  <div class="Calculator" align="center">
    <input type="text" name="text1" id="a1">Enter 1st Number
    <br>
    <br>
    <input type="text" name="text2" id="a2">Enter 2nd Number
    <br>
    <br>
    <button onclick="add()">Add</button>
    <p id="p1"></p>

    <script type="text/javascript">
      function add() {
        var a = document.getElementById("a1").value;
        var b = document.getElementById("a2").value;
        var total = parseInt(a) + parseInt(b);
        document.getElementById("p1").innerHTML = total;
      }
    </script>

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.