1

I'm working on a JavaScript project and I would like to have your help/advices. I have to create an "assistant" to help to create formulas. To explain, I have a button to add a component to the formula (add input). A component is composed of 3 sub-components (last input doesn't appear in the formula).

I would like to update the formula when something change in the components. For example, I would like to have: 0.1*levenshtein(name) + 0.5*distance(coordinates) + 0.4*levenshtein(website).

current version I have tried some code but I can't have something working. My code is working for inputs when they have constant (predefined) values but doesn't work when user changes values. Instead of having code, I'm looking for advices or how I can implement this problem. If needed I can provide some code (but it can be a little tricky for you who doesn't know better my project)...

Thanks in advance,

Have a good weekend

Nelly Barret
  • 144
  • 1
  • 17

1 Answers1

1

Without your html it is indeed impossible to give you a specific code.

As of a high level solution to your described issue, which is updating a formula when the user change input values - the solution will be adding event handlers to the formula components (input and select elements) which will initiate the formula calculation function (Same function initiated when clicking the send button) when those events are fired.

You will have to add the event handles dynamically to the newly added components which catch a change in each component (Maybe as a start it is the quickest and simplest approach), or add an event listener to the component's parent container which will catch a change in each of it's children. Again, without the html I cannot give any specific code here.

Anyway, you will most likely need to utilize the change and on functions of jQuery. See some good examples here and here

Alon Adler
  • 3,984
  • 4
  • 32
  • 44
  • Hi, a lot of thanks for your response. I have already added events to my inputs and selects. I will give a look to your examples. My problem is to update my formula each time the user changes something. Have a nice evening and thanks for your response. – Nelly Barret Apr 28 '19 at 17:29
  • If events are not the issue nor the solution here then please explain more precisely what is the problem which you encounter when you try to _update my formula each time the user changes something_. – Alon Adler Apr 28 '19 at 18:52