Sending hidden dynamic data using onBeforeSave

I found the following post about passing hidden data:

The code below works for passing data along with a form submission, which is fantastic:

$w('#dataset1').onBeforeSave(() => {
	$w('#dataset1').setFieldValue('jobApplied', '#text18');
	$w("#dataset1").save();
});

The only problem I’m running into is that it’s literally passing “#text18” for the field. I get it, the system is doing as it’s told. I just can’t figure out how to pass an actual piece of dynamic text to the dataset. What’s the formatting needed for that?

I’ve tried:
$w(‘#dataset1’).setFieldValue(‘jobApplied’, ‘(#text18)’);
$w(‘#dataset1’).setFieldValue(‘jobApplied’, (‘#text18’));
$w(‘#dataset1’).setFieldValue(‘jobApplied’, ‘<#text18>’);

But they all just print normal text. I’m wondering if the formatting is wrong, but also if the Field Type needs to be changed. What would be best to receive this data?

For context, #text18 is coming from #dataset2 and the submit button is adding a new line to #dataset1 with additional user-supplied information. I’ve tried setting the Field Type for jobApplied to ‘Reference’ with the Referenced Collection’ set to #dataset2. It should just be a string of numbers by the time it is actually received by #dataset1.

To summarize:
Problem 1: How should I format the second argument within .setFieldValue to pass dynamic data?
Problem 2: How should I set the Field Type to receive this data?

if i understood correctly, you want to get the content of a textfield with id #text18 ’ and insert it.
this can be done with the $w selector:

$w( #text18 ').text
FieldType should be text.

Thanks for replying, Moshe. I’ve confirmed the FieldType is text, but I’m still not getting the values from the page into the dataset.
I changed one value slightly, so we can pass multiple FieldValues in one go:


$w.onReady(function () {
$w('#text18').text; 
$w('#text15').text; 
$w('#dataset1').onBeforeSave(() => {
	$w("#dataset1").setFieldValues( {
	      "jobApplied":  "#text18",
	      "jobTitle":   "#text15"
    } );
	$w("#dataset1").save();
});
});

This still passes “#text18” (and “#text15”) to #dataset1.

As I said, if you look at t he API for setFieldValues you will see that the value needs to be the real string you want to apply and not an object id like you specified.

try using $w(’ #text18 ').text instead of plain “#text18

I apologize if I misunderstood. Attempting the following:

	$w("#dataset1").setFieldValues( {
	      "jobApplied":  $w(#text18').text,
	      "jobTitle":   $w(#text15').text
    } );

returns nothing for the fields of jobApplied and jobTitle. Putting double quotes around “$w(#text18’).text” does pass text to those fields, but the text that’s within the quotes. I believe this is expected.

edit: The mistake was using a single quote, or apostrophe, within the parentheses. The below works perfectly:

	$w("#dataset1").setFieldValues( {
	      "jobApplied":  $w('#text18').text,
	      "jobTitle":   $w('#text15').text

Thanks for your help, Moshe!

Anytime, glad I could help :slight_smile:
Thanks for noticing my typo, fixed the original comment for future readers!