2

I am creating a small webpage that will add two input fields together and place the result in another input field. This is what I have:

<html>
<head>
 <title>Calculator</title>
 <script type="text/javascript">
 function add(){
 var num1 = parseInt(document.calc.num1.value);
 var num2 = parseInt(document.calc.num2.value);
 var answer = (num1+num2);
 document.getElementById('res').value = answer;
 }
 </script>
</HEAD>
<BODY>
 <FORM NAME="calc">
 <INPUT TYPE ="button" NAME="add" Value="+" onClick="add()">
 <hr/>
 <INPUT TYPE ="text" NAME="num1" Value="">
 <INPUT TYPE ="text" NAME="num2" Value=""> 
 <hr/>
 <INPUT TYPE ="text" ID="res" NAME="result" VALUE="">
 </FORM>
</BODY>
</HTML>

And I am getting the following error when I press the + button.

Uncaught TypeError: object is not a function 
asked Mar 10, 2014 at 17:01
1
  • One thing I would suggest that MAY alleviate the problem is to pick a case, upper or lower, and stick with it. Most browsers prefer lower case for elements. Consider: stackoverflow.com/questions/4380719/onclick-or-onclick Commented Mar 10, 2014 at 17:11

5 Answers 5

3

Try changing the function name from add to addNumbers or something like that.

answered Mar 10, 2014 at 17:11
Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant! I suppose the issue was because my function and button name were the same? Changing the function name to addNumbers solved the issue. Thank you.
Yes; add in the handler referred to the element.
Yeah. See this SO question too. stackoverflow.com/questions/15375845/…
0

onclick is the right attribute to handle click

answered Mar 10, 2014 at 17:09

Comments

0
onClick="add()" 

Switch this bit to

onclick="add()"
answered Mar 10, 2014 at 17:09

2 Comments

No; HTML attributes are case-insensitive.
as @SLaks mentioned, changing the Click to lowercase did not solve the issue.
0

The problem is the name of the function "add()", change the name and you will see that it will works!

answered Mar 10, 2014 at 17:13

Comments

0

HTML

<p>
 <label for="field1">Field 1</label>
 <input type="number" id="field1"/>
</p>
<p>
 <label for="field2">Field 2</label>
 <input type="number" id="field2"/>
</p>
<p>
 <label for="total">Total</label>
 <input readonly type="number" id="total"/>
</p>
<p>
 <input type="button" id="calc" value="Calculate"/>
</p>

Javascript Goes in a script tag in head.

function sumFields(fields) {
 var total = 0;
 // goes through each field and adds them to the total
 for(var i=0,l=fields.length; i<l; i++)
 { total += parseInt(document.getElementById(fields[i]).value); }
 document.getElementById('total').value = total;
}
function calc_click() {
 // runs when the button is clicked
 sumFields(['field1','field2']);
}
// main function
function init() {
 // add button functionality
 document.getElementById('calc').addEventListener('click',calc_click,false);
}
// fires when the DOM is loaded
document.addEventListener('DOMContentLoaded',init,false);
answered Mar 10, 2014 at 17:14

2 Comments

Performance articles tend to "suggest" you put as much of your JS at the bottom/footer in your doc.
Might as well start going back to document.write() while we're at it, because it's faster. Sorry, it's still bad form.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.