+ 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.
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.
+ 1
pseudocode
button.onclick=func add()
a=input1.value
b=input2.value
display a+b
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