-2

How can I calculate numbers inside a input(textbox) using jquery? Currently I achieve this this by using eval.

onChange="MyForm.inputID.value = eval(MyForm.inputID.value)

Ex: 2+4 onChange results 6

How can I achieve this using jquery on all the inputs without using IDs.

Thank you!

alien250
  • 49
  • 6
  • The code you're showing right now does nothing, since it sets the value of the input to the value of the input. If you want the value of the input as a number, there are many questions and answers about how to do that in JavaScript on the site already. See for example [How to get a number value from an input field?](https://stackoverflow.com/q/28695617/215552). – Heretic Monkey Oct 28 '19 at 21:14

1 Answers1

1

In case someone has the same question, I figured it out:

$(document).ready(function(){
$('input').blur(function(){
$('input').each(function(e) {
try{
    var ans = eval($(this).val()).toFixed(2);
    $(this).val(ans);
  } catch(e) {}
});
});
});
alien250
  • 49
  • 6