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

Discover More Reads

Real Stories & Real Success

Do you have a tech idea?

Let’s talk!

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

If you prefer email, write to us at hello@jetrockets.com