JavaScript: Put a Calculated Value and Put it in a Text Box


Userlevel 4
Badge +10

Hi Folks,

I am creating a form signature feature on one of my forms and I want to get the current user display name to show up in the signature block. Here is what I have so far:

I have a calculated Value that gets exactly what I want...

193154_pastedImage_1.png

I want to get the values from the Calculated Value control into the following JavaScript so I can trigger it with a button click"

function signFormPM() {
    var curDate = new Date();
    NWF$('#'+ jsvar_PMDate).val((curDate.getMonth() + 1) + '/' + curDate.getDate() + '/' + curDate.getFullYear());
    NWF$('#'+ jsvar_PMName).val("[PUT THE CALC VALUE HERE](Current User)");
}

Any thoughts?


10 replies

Userlevel 5
Badge +14

assign a javascript variable to the calculated value control, eg. calcCtrl.

then you should be able to get its value from javascript with

NWF$('#'+calcCtrl).val()

Userlevel 4
Badge +10

Hi Marian Hatala,

Does this look like it should work then? The button click is failing when this function is called.

function signFormPM() {
    var curDate = new Date();
    NWF$('#'+ jsvar_PMDate).val((curDate.getMonth() + 1) + '/' + curDate.getDate() + '/' + curDate.getFullYear());
    NWF$('#'+ jsvar_PMName).val(NWF$('#'+ jsvar_CurUserPreferredName_CAL).val() +" (Current User)");
}‍‍‍‍‍‍‍‍‍‍
Userlevel 5
Badge +14

yes, seems to.

I tried your line of code for current user and it worked for me.

what error do you get?

Userlevel 4
Badge +10

No explicit error, rather, when I clicked the "Sign" button, the page refreshed and no data was rendered in the appropriate fields. When my code does not error, a button click will immediately pop values into my fields.

Also... this code works:

function signFormPM() {
    var curDate = new Date();
    NWF$('#'+ jsvar_PMDate).val((curDate.getMonth() + 1) + '/' + curDate.getDate() + '/' + curDate.getFullYear());
    //NWF$('#'+ jsvar_PMName).val(NWF$('#'+ jsvar_CurUserPreferredName_CAL).val() +" (Current User)");
}

...and this code des not:

function signFormPM() {
    var curDate = new Date();
    NWF$('#'+ jsvar_PMDate).val((curDate.getMonth() + 1) + '/' + curDate.getDate() + '/' + curDate.getFullYear());
    NWF$('#'+ jsvar_PMName).val(NWF$('#'+ jsvar_CurUserPreferredName_CAL).val() +" (Current User)");
}

The only difference being I commented out the line calling the calculated value.

Userlevel 5
Badge +14

hm, that's weird.

what's the value of calculated control? doesn't it contain any 'possibly dangerous' characters that might break js code?

keep the current user line commented out and try one by one whether following executes correctly

console.log("1: "+NWF$('#'+ jsvar_CurUserPreferredName_CAL).val());

console.log("2: "+NWF$('#'+ jsvar_PMName).val());                                

NWF$('#'+ jsvar_PMName).val("xxx");

Userlevel 4
Badge +10

Hi Marian,

Calculated Control formula is: userProfileLookup(Current User, "PreferredName")

In my case it would render "Kelligan, Patrick S."

Here are your code results

console.log("1: "+NWF$('#'+ jsvar_CurUserPreferredName_CAL).val()); 
    //Error

console.log("2: "+NWF$('#'+ jsvar_PMName).val());
    //No error but no value population

NWF$('#'+ jsvar_PMName).val("xxx");
     //Populates :XXX" into the signature field
Userlevel 5
Badge +14

any details on the error? (developer console message, message popup...)

can you in addition test:

console.log("4: "+NWF$('#'+ jsvar_CurUserPreferredName_CAL).length);

console.log("5: "+jsvar_CurUserPreferredName_CAL);

can you proof-check you have correctly configured js variable for calculated control (eg. some typo)?

what's the dataype is set for calc value control?

what form mode do you test on?

hopefully you tested on published form not only on preview

Userlevel 4
Badge +10

Hello Marian,

can you proof-check you have correctly configured js variable for calculated control (eg. some typo)?

Funny thing...

I changed a setting on the calc value a while back and it had some unintended consequences. The calculated value control is not meant to be seen so I set the Visible property to No and it made the js variable go away. Is did not delete the variable string but hid the configuration input box.

193190_pastedImage_3.png

When I set it back to Yes, the js variable showed up again and the original suggestion you made worked.

NWF$('#'+ jsvar_PMName).val(NWF$('#'+ jsvar_CurUserPreferredName_CAL).val() +" (Current User)");

I am sorry for the false errors. Thanks so much for getting this thing figured out!

Userlevel 5
Badge +14

great!

yes, that's a known 'feature'.

the best way to manage visibility is to place a control into panel and assign rule to the panel.

Userlevel 4
Badge +10

I do that in some cases. I just have the calculated value in the header. I will bush it behind the banner.

Thanks again for your help Marian!

Reply