How to do this Simple Calculation using JavaScript ? Because i am a beginner.
i want to calculate the value in the same input field.
e.g In the input field, I type the value such as ” 3+4*2 “, then i click the button, the same input field will show the result like “11”.
Here is my code, but not work.
my input type = “text” and id =”expr”
my button type = “button” , value = “Calculate”, onclick = “getAnswer();”
Here is my javascript code.
function getAnswer(){
val = eval(document.expr.value);
document.expr.value = val;
}
but i’m not work.
document.exprmost likely does not exist.Give your input tag an id and use
document.getElementById('blah')instead or usedocument.forms.FORMNAME.exprif the field is inside a form.Demo: http://jsfiddle.net/ThiefMaster/kRdJH/