{"id":1470,"date":"2024-03-12T17:06:25","date_gmt":"2024-03-12T20:06:25","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=1470"},"modified":"2024-03-18T19:38:09","modified_gmt":"2024-03-18T22:38:09","slug":"updating-input-and-triggering-onchange-event-in-react","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/updating-input-and-triggering-onchange-event-in-react\/","title":{"rendered":"Updating Input and Triggering onChange Event Programmatically in React"},"content":{"rendered":"<p>While it's easy to update an input's value using a ref, that doesn't trigger the input's onChange event, which may be an issue if you're relying on onChange to fire when the input value changes.<br \/>\nIf you <em>don't<\/em> need the onChange event to fire, this might be good enough:<\/p>\n<pre><code>if (props.inputRef) { \/\/ props.inputRef is a reference to the input\n    props.inputRef.current.value = \"new value\";\n}\n<\/code><\/pre>\n<p>But if you <em>do<\/em> need the onChange event to fire, this workaround sets the value <em>and<\/em> triggers the event:<\/p>\n<pre><code>const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;\nnativeInputValueSetter?.call(props.inputRef?.current, value); \/\/ props.inputRef is a reference to the input\nconst event = new Event('change', { bubbles: true });\nprops.inputRef?.current?.dispatchEvent(event);\n<\/code><\/pre>\n<p>Source: https:\/\/stackoverflow.com\/a\/46012210\/4669143<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While it&#8217;s easy to update an input&#8217;s value using a ref, that doesn&#8217;t trigger the input&#8217;s onChange event, which may be an issue if you&#8217;re relying on onChange to fire  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[28],"tags":[32,31,33,30],"class_list":["post-1470","post","type-post","status-publish","format-standard","hentry","category-react","tag-event","tag-input","tag-onchange","tag-react"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-nI","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/1470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=1470"}],"version-history":[{"count":5,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/1470\/revisions"}],"predecessor-version":[{"id":1503,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/1470\/revisions\/1503"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=1470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=1470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=1470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}