Javascript get and assign values from form fields

How to get and assign values from form fields using JavaScript.

Overview

The issue is not knowing the correct JavaScript syntax to retrieve values from different sources like plain fields, database-loaded fields, or model variables, and assign them to other elements.
To retrieve a value, ensure the source element is rendered on the page, then use methods like getElementById to access its content. Assign the retrieved value to a target element by setting its value property accordingly.

Answered
ChronoForms v7
vd vdneut 08 Feb, 2023
I'm looking for the Javascript syntax to get the value from and assign a value to:
- A plain form field like a text field
- A form field read from the database like {Model.field_name}
- A variable available when the Set Models Data in the Read data action is set.

Any lead on one of the above is highly appreciated!
wb wbuk 12 Feb, 2023
Answer
3 Likes
Where is the javascript getting the value from? It needs to be already rendered on the page for the script to get the value and assign it elsewhere (eg the value is in a div somewhere on the page).

Let's say your page looks like this:


<div id="value">1234</div>
<input type="text" name="amount" id="field">


To retrieve it from the div and place it into input field:


let val = document.getElementById("value").innerText;
document.getElementById("field").value = val;
vd vdneut 18 Feb, 2023
Oh, my, I feel like a beginner... Maybe I am🙂
Thanks!
This topic is locked and no more replies can be posted.