What I intend is to take the numbers of a <input>
and then operate them directly from JavaScript:
If, for example, <input>
it is entered ((2+2)/4)*3
, the answer is:3
Solution Based on the answers received, I concluded the following solution to the question:
JavaScript:
function calc() {
//Coger el valor del input
var value = document.getElementById("value").value;
//Operar los datos recogidos
var operation = eval(value);
//Mostrar el resultado
alert(operation);
}
HTML:
<body>
<input id="value" type="number">
<button id="calculate" onclick="calc()">Calculate</button>
</body>
You could collect the value directly from the input and use the
eval
.You can use the Function constructor :