<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>How to set the value of Liferay 7.4 Forms fields from JavaScript on a page?</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=121831988" />
  <subtitle>How to set the value of Liferay 7.4 Forms fields from JavaScript on a page?</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=121831988</id>
  <updated>2026-04-05T22:51:30Z</updated>
  <dc:date>2026-04-05T22:51:30Z</dc:date>
  <entry>
    <title>How to set the value of Liferay 7.4 Forms fields from JavaScript on a page?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121831987" />
    <author>
      <name>Andy Tims</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121831987</id>
    <updated>2023-06-01T14:40:35Z</updated>
    <published>2023-06-01T09:50:56Z</published>
    <summary type="html">&lt;p&gt;What I want to know is how to set the value of Liferay Forms fields
  from JavaScript on a page.&lt;/p&gt;
&lt;p&gt;In Liferay 7.1 this could be done simply, eg:&lt;/p&gt;
&lt;pre&gt;
&lt;code class="language-javascript"&gt;    var textfield2 = document.querySelector('[id*=&amp;quot;$Text49276926$&amp;quot;]');
    textfield2.value = &amp;quot;Hello from JS control&amp;quot;;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This still works initially in 7.4, but as soon as the user clicks
  anywhere on the page, the new value is overwritten by the old value.&lt;/p&gt;
&lt;p&gt;I believe that it is ReactJS that is doing this.&lt;/p&gt;
&lt;p&gt;What I want to know is whether you can see any problems with using
  the example JS below, that does work?&lt;/p&gt;
&lt;p&gt;Or is there a &amp;quot;ReactJS&amp;quot; way of updating form fields, ie by
  doing an import of ReactJS libraries and using some functionality from them?&lt;/p&gt;
&lt;pre&gt;
&lt;code class="language-javascript"&gt;var mutationObserver = new MutationObserver(function(mutations) {
    console.log(&amp;quot;HELLO ANDY&amp;quot;);
    var textfield2 = document.querySelector('[id*=&amp;quot;$Text49276926$&amp;quot;]');
    setNativeValue(textfield2, &amp;quot;DOES THIS WORK????&amp;quot;);
});

mutationObserver.observe(document.body, {childList: true, subtree: true});

function setNativeValue(element, value) {
    let lastValue = element.value;
    element.value = value;
    let event = new Event(&amp;quot;input&amp;quot;, { target: element, bubbles: true });
    let tracker = element._valueTracker;
    if (tracker) {
        tracker.setValue(lastValue);
        console.log(&amp;quot;in here&amp;quot;);
    }
    element.dispatchEvent(event);
}

/* The above JS was taken from first answer on https://stackoverflow.com/questions/30683628/react-js-setting-value-of-input

This links to https://github.com/facebook/react/issues/11488 where the above technique is described as a hack.

This, https://dev.to/poeticgeek/in-react-component-controls-you-4ed8, explains how React uses a 'valueTracker' and gives the above technique as one solution.

This, https://www.pluralsight.com/guides/how-to-use-react-to-set-the-value-of-an-input, explains how to use React to set the value of an input, but it is for a React component that is created in a particluar way, whereas we are stuck with however LR Forms creates its React components.
*/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;</summary>
    <dc:creator>Andy Tims</dc:creator>
    <dc:date>2023-06-01T09:50:56Z</dc:date>
  </entry>
</feed>
