question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No onChange event triggered when changing value using javascript

See original GitHub issue

Prerequisites

  • I have read the documentation;
  • In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Description

In my code I use document.getElementById(aFormInputId).value = newvalue to update an input in a react-jsonschema-form. The new value comes from a different part of the application and rather than deconstructing the form id and updating that value in formData and then updating the form, this seemed simpler.

Settings the value works fine, but the form’s onChange isn’t triggered. If I trigger blur on the input afterwards, that event is called correctly but it doesn’t bubble up to a change event on the form itself.

What is the preferred way to force an onChange event on a react-jsonschema-form?

Steps to Reproduce

  1. call document.getElementById(aFormInputId).value = newvalue
  2. see the value in the input change
  3. call const event = document.getElementById(aFormInputId).dispatchEvent(new Event('blur', { bubbles: true }));
  4. see the onBlur event get called as expected
  5. see no onChange even being called

Expected behavior

When the value of an input changes problematically, the form’s onChange event is also called

Actual behavior

The forms onChange event is not called and formData is not updated.

Version

1.0.3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
knilinkcommented, Jun 7, 2018

because assigning value doesn’t trigger an input event. try

element = document.getElementById(aFormInputId)
element.value = newvalue
element.dispatchEvent(new Event('input', { bubbles: true }))

read more

3reactions
KenEuckercommented, Jul 21, 2018

I’m also struggling to programmatically set the inputs of my form from outside the App and getting the react-jsonschema-form to recognize these changes. I’ve tried the suggestions here, by doing:

field.value = value;
field.dispatchEvent(new Event('input', { bubbles: true }));

But that still doesn’t work and the form believes that there are errors even though the required fields do have a value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery - I set field value w/ javascript, onchange not triggered ...
and I have an onchange event like this that I would like to fire upon my field update as of above. $('#data_1').change(function(){ img...
Read more >
How to trigger onchange event on change of value from...
Hi all. i have the workflow which change the field value of entity in the form inside the form on change of this...
Read more >
How can I trigger an onchange event manually in javascript?
You can dispatch events on individual elements using the dispatchEvent method. Let's say you have an element test with an onChange event −...
Read more >
How to trigger an onchange event? - Vue Forum
Maybe using watch in the component ? watch { value(value) { this.$emit('onchange', value); // or generate/simulate a native events (not sure ...
Read more >
How to trigger onchange event on input type=range while ...
Onchange : Onchange executes a JavaScript when a user changes the state of a select element. This attribute occurs only when the element ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found