Non-tech founder’s guide to choosing the right software development partner Download Ebook
Home>Blog>Onchange trigger example with react

onChange trigger example with React

Recently, I faced the issue. There is a form with some fields. Each field has several functions; functions do something. For example, one function writes the field name and its value into the object. Then the customer asks to add the buttons with specified values. When the button is clicked, the relevant field should update its value.

Here is the trigger hack that calls the onChange function of that field.


function triggerInput(enteredName, enteredValue) {

  const input = document.getElementById(enteredName);



  const lastValue = input.value;

  input.value = enteredValue;

  const event = new Event("input", { bubbles: true });

  const tracker = input._valueTracker;

  if (tracker) {

    tracker.setValue(lastValue);

  }

  input.dispatchEvent(event);

}

Live example link

Categories:

Discover the Products We’ve Developed

Let’s Build Something Great Together

Let’s discuss your project and see how Ruby on Rails can be your competitive advantage.

*By submitting this form, you agree with JetRockets’ Privacy Policy