I have been searching for a way to type a formula into a single text input and when I hit the Enter key, the formula executes and the result replaces the formula in the same input box.
Really I want it to function exactly like an Excel cell. https://formulajs.info/functions/ does what I want, except there is a button to click and the result comes back in a separate input box.
I'm learning to write code, so I don't have the skills to reverse-engineer that plugin, and that was the only thing I could find online that was even close. Most of my searches just keep turning up grid spreadsheets with formulas and it is important for me to keep it to a single input.
Does anyone have any ideas of where I might find something already made or how I could make something like that for myself? I don't need all the fancy engineering, financial, etc types. I just want simple math with an option for parentheses to nest formulas, ie 2*6 (8/4) gives a result of 14.
CodePudding user response:
You can try this for simple JavaScript
const input = document.getElementById('formulaInput');
input.addEventListener('keydown', (event) => {
if (event.keyCode === 13) { // Enter key Code
const formula = input.value;
const result = eval(formula);
input.value = result;
}
});