Collect initial text from input field

Hi
I have a webpage that generates text in a textbox based on what users check of in a dropdown menu.
When users contact me, I want to collect the information in that textbox. I have created a connector that collect all the data I want to collect. And created an input field that copy the value of the textbox, so that I can collect the value from the textbox as well.

$w("#input9").value = $w("#text49").text;

When I submit, the value in does not get collected if I do not also change it before submitting. Why does not the chosen value get collected if the field is not edited before submission? Any tips to how I can collect this data, without user interference in the input field?

This previous post should help with this.
https://www.wix.com/corvid/forum/community-discussion/is-it-possible-to-use-wix-text-input-to-display-the-contents-of-a-collection

If it doesn’t then post back.

Thanks for helping. I found another post recommending

$w("#dataset8").setFieldValues( {
...
...
})

That solved the problem :slight_smile:

@eskildthorgeirsson
Great, don’t forget to include a save function if you are using code to save it all and not a submit button.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValues

@givemeawhisky Thanks again

export function button8_click(event) {
$w(“#dataset8”).setFieldValues( {
"education ": $w(‘#input9’).value,
“membership”:$w(‘#input7’).value,
“work”: $w(‘#input8’).value,
} );}

I use this formula. Button8 is the submit button. In addition, the input fields are also connected to the database through a connector. I guess the code should be unnecessary, but this is the only way I got it working.

@eskildthorgeirsson
It is needed as you in your original post you are simply adding the value through code, which does not make the element that you are using register a value being inserted, so basically the onChange event is not being registered.

Only if you clicked anywhere on the page itself away from the element would it then register the value being added and work as it should.

That is why you need to use the setFieldValue function here.

https://www.wix.com/corvid/reference/$w.TextInput.html#value

value

Sets or gets a text input’s value.

Description
To reset the text input, restoring any placeholder text, set the value property to null or undefined.

Note
Changing a text input’s value in code does not trigger an onChange event.
Using the Input Settings panel in the Editor you can set the type of a text input. That type is used only for validation purposes. The value returned by the value property of a text input is always a string, regardless of the type set in the Editor.

If an element is connected to a dataset, setting the element’s value in code does not set the value of the connected field in the dataset. That means if you use the dataset to perform a submit, the value changed in code is not reflected in the submitted item.

To submit the new value using a dataset, set the field’s value using the setFieldValue() function before performing the submit.

Took me a while to find this solution, thanks.

$w.onReady( function () {
getDates();

});
export function diff_days() {
let dt1 = new Date($w("#datePicker1").value);
let dt2 = new Date($w("#datePicker2").value);
return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
}

export function datePicker1_change_1(event) {
let date1 = $w("#datePicker1").value;
getDates();
}
export function datePicker2_change_1(event) {
let date2 = $w("#datePicker2").value;
getDates();
}

function getDates(){
let diff = diff_days();
$w("#daysDiff").text = String(diff);
 $w("#days").value = $w("#daysDiff").text;
  console.log(diff)
}

export function button7_click(event) {
    $w("#dataset1").setFieldValues( {


"days": $w('#days').value,

} );}

Took me awhile to get this final solution to get length in days of Major Projects.


$w.onReady( function () {
getDates();

});
export function diff_days() {
let dt1 = new Date($w("#datePicker1").value);
let dt2 = new Date($w("#datePicker2").value);
return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24));
}

export function datePicker1_change_1(event) {
let date1 = $w("#datePicker1").value;
getDates();
}
export function datePicker2_change_1(event) {
let date2 = $w("#datePicker2").value;
getDates();
}

function getDates(){
let diff = diff_days();
$w("#daysDiff").text = String(diff);
 $w("#days").value = $w("#daysDiff").text;
  console.log(diff)
}

export function button7_click(event) {
    $w("#dataset1").setFieldValues( {


"days": $w('#days').value,

} );}