0

I have an equation/formula stored in database and I want it to be triggered based on key up input event in a webpage.

Example formula: [55-57]

This is a simple minus operation, where the number actually represents the id of a row in database

I have looked at this solution which replaces numbers found in a string to new value. But I need the new value to be replaced with incremented letters such as a, b and so on. Also the leading and ending brackets [] need to be removed so that I can perform an eval later using JavaScript.

Later the equation will be convert to a-b. Variable a and b represent other HTML elements that holds a value. So whenever I key in something into text field, changes will reflect on other part of webpage. It's like auto computation.

Thank you for those helping this. Hope this question will help somebody.

9
  • Please rephrase your question, or give examples. It is very confusing as is. Commented Jun 15, 2014 at 3:33
  • what don't you understand? I have change it a bit Commented Jun 15, 2014 at 3:37
  • 1
    So you want <value of #55> - <value of #57> ??? Yeah, that's not the slightest bit clear in your question. What do you mean by "incremented letters"? The same thing, e.g. #a? Commented Jun 15, 2014 at 3:46
  • 1
    @MuhaiminAbdul you should really try to organize a bit what you want to do so that you can extract the important part of the problem for us to help you with. Commented Jun 15, 2014 at 3:51
  • 1
    I try harder to organize the question above. Thanks Commented Jun 15, 2014 at 3:58

1 Answer 1

1

Try something like this. If you need more help, you seriously need to re-word your question or post a jsfiddle, or something.

var eqn = '55-57'; // brackets removed.  Remove them with a regex of /\[|\]/g if you need to

var result = eval( eqn.replace( /\w+/g, function( res ){
    return +document.getElementById( res[1] );
} );

Basically this replaces 55 and 57 with the numerical values of #55 and #57. It would also work for #b, etc.

It then eval's the result, basically doing whatever math is in your equation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.