0

Operators inside input field

I have a function that doesnt work if i write an operator inside the input field. Ex <Input type = "number" id= number> Now i wrote a function on click called calculate <Script> Function calculate { var number = document.getElemntById("number").value; alert (number); } </script> The function works if i write a number inside the input field but it doesnt work when i write an operator inside the input field. I want to calculate the value of the input value and alert it Ex 3+3 output 6

6th Mar 2019, 7:32 PM
sSkills
sSkills - avatar
4 Respostas
+ 3
You could use the eval function: var number = eval(document.getElementById("number").value); alert(number); Though I'd advise against using it for reasons stated here (also alternate methods are provided): https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea
6th Mar 2019, 7:42 PM
LynTon
LynTon - avatar
+ 3
Ok, sorry it took a while but here you go: https://code.sololearn.com/WG37L0Nt9QTT/?ref=app
6th Mar 2019, 10:14 PM
LynTon
LynTon - avatar
+ 1
Eval was working in the beginning but when i tried to divide 2 diffrent variabels, eval doesnt work anymore. The script which is not working is If ( v !="" && s != "" && t =="") { t = eval(s)/eval(v) document.getElementById("t").value = t; } This script doenst work with eval method. It works without using operators
6th Mar 2019, 7:56 PM
sSkills
sSkills - avatar
0
Thank you for ur help but it is till not working when i write the script into my website. When i add operators into an input field the script will run the else statment instead of if statment. when i alert the value of numberV oe numberS after using operators the value is always undefined
7th Mar 2019, 1:18 PM
sSkills
sSkills - avatar