I am having this function inside script.
function arith()
{
var n1 = parseInt(document.getElementById('num1').value, 10);
var n2 = parseInt(document.getElementById('num2').value, 10);
var newVal;
if(op == "Operation 1")
{
newVal1 = n1 + n2;
}
else if(op == "Operation 2")
{
newVal2 = n1 - n2;
}
else if(op == "Operation 3")
{
newVal3 = n1 * n2;
}
else if(op == "Operation 4")
{
newVal4 = n1 / n2;
}
var demoP = document.getElementById("demo");
{
demoP.innerHTML = "Operation 1=" + newVal1;
demoP.innerHTML = "Operation 2=" + newVal2;
demoP.innerHTML = "Operation 3=" + newVal3;
demoP.innerHTML = "Operation 4=" + newVal4;
}
return false;
}
When I call these innerHTML, can I use <p id="demo"></p>
Is it enough or I need to call each elements?
newValXvariables will be assigned a value within that function (because of the if/else if structure). Are they global variables? If they're not declared somewhere you'll get a reference error when you try to use the ones that haven't been assigned a value.