+ 1

getting syntax error and function not defined error

I am trying to write a code that adds the value of two input fields with a function , that should be triggered when clicking on the button.

5th Sep 2017, 10:01 PM
Franciscano
Franciscano - avatar
3 Antworten
+ 1
the keyword var is only used to declare a variable. Don't use var when just using the variable. in the future please provide a link to your code to make it easier for us to find.
6th Sep 2017, 12:10 AM
Jordan Chapman
Jordan Chapman - avatar
+ 1
pseudocode button.onclick=func add() a=input1.value b=input2.value display a+b
6th Sep 2017, 7:54 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
0
Your problem is that you are using "var" every time you use a variable, but that's only needed when declaring them. Also you are trying to use the variable "x" without initializing it before. Here's the code that causes problems: function sumar() { document.getelementById("num1"); console.log(var x); //unitialized variable x var y=document.getelementById("num2").value; document.getelementById("resu").innerHTML = var x + var y; //using "var" when not required } Here's fixed: function sumar() { var x = document.getelementById("num1"); //declare and initialize var x console.log(x); //using var x, no need of var because it's already declared. var y = document.getelementById("num2").value; document.getelementById("resu").innerHTML = x + y; // same as before, x and y are already declared } This should fix all syntax errors
6th Sep 2017, 12:14 AM
Daniel de Lizaur