1

I have an input in a form built with aura (Salesforce JS framework):

<input class=" input uiInput uiInputText uiInput--default uiInput--input" type="text" aria-describedby="5284:0" placeholder="" id="7:4790;a" data-aura-rendered-by="17:4790;a" data-aura-class="uiInput uiInputText uiInput--default uiInput--input" data-interactive-lib-uid="54" aria-required="true">

I need to change the value of this input using javascript. However, when doing:

document.getElementById("7:4790;a").value = "random value";

Visually, it changes the value in the input, but it is not taken into account when saving as if I didn't change anything.

How can I achieve this ? Do I need to trigger a specific event so that aura takes notice of the new data ?

Graham Slick
  • 6,692
  • 9
  • 51
  • 87
  • The `change` event can be used here. [See @ MDN](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onchange) – Brian Peacock Apr 04 '19 at 08:46
  • I don't know "aura" at all. But I can guess that the framework is listening to the input events so it's not enough to just change the value. There is a similar [issue](https://stackoverflow.com/a/22942828/863110) maybe it can give you a direction to the solution. Also, there is dedicated stackexchange [site for salesforce](https://salesforce.stackexchange.com/) maybe they could give you better answers. – Mosh Feu Apr 04 '19 at 09:09

1 Answers1

0

You need to create an attribute of string type.

<aura:attribute name="myInputValue" type="String" />

Pass the attribute to the value property of the input tag.

<input .... value="{! v.myInputValue}" />

Now, whenever you want to change the value in the input, you can simply do this from your javascript function:

component.set('v.myInputValue, "My new String" />

Important Note: Salesforce frameworks don't allow DOM level manipulation due to a security architecture called Locker service. It is not advisable to follow DOM level Manipulation. Instead, follow the state-based approach like above.