How can I set a value in a text field using JavaScript?

  • 3 April 2017
  • 4 replies
  • 39 views

Badge +5

I have searched the Nintex Community and tried many of the things that were recommended.  Unfortunately, I still haven't had any luck.  I'm trying to auto-populate two text fields: First Name (JavaScript ID "varFirstName") and Last Name (JavaScript ID "varLastName").  I have two calculated fields (which I will later hide when the form goes into production) that get the first and last names of the current user using User Profile Services (lookup formulas are: userProfileLookup(Current User,"FirstName") and  userProfileLookup(Current User,"LastName")).  The calculated fields show the names with no problems.  However, I can't add these values to the two text fields.  This is the JavaScript custom code that I am using.  

NWF.FormFiller.Events.RegisterAfterReady(function () {
var firstName = NWF$('#' + varUserFN).val();  //this refers to the calculated field for first name
var lastName = NWF$('#' + varUserLN).val();  //this refers to the calculated field for last name

NWF$('#' + varFirstName).val(firstName).trigger('blur');
NWF$('#' + varLastName).val(lastName).trigger('blur');

});

Note that I originally added this code inside of  "NWF$(document).ready(function(){})" as it is more proper to stick with all jQuery or all straight JavaScript, right?  But, neither worked.  I've also tried the suggested from this previously posted question:   .  I hadn't had success with that, either.  What could I be doing wrong?  Any help would be much appreciated!  Thanks in advance!


4 replies

Badge +4

Hi Angela,

Can you try to create function in custom javascript section in form settings and call that function from calculated field control. E.g.

  1. Create function with any name inside Form Settings >> Custom Javascript

     function setFirstName(firstName){
           NWF$('#' + varFirstName).val(firstName);
         } 

   2. Open your calculated-field for firstname and call above function from Formula. 

      setFirstName(userProfileLookup(Current User,"FirstName"));

   

This should work.

Regards

Shirin

Userlevel 5
Badge +14

I think you read value of calculated value control before it is really populated/calculated.

I would say if you moved your code to a separate function and called the function on button click (ie. after form is loaded) it would work.

I would suggest to follow approach from the link you referenced, that will ensure that actual calculated value control is copied over to a text box with every change of calculated value.

if you have any specific problem with that please post details.

Badge +5

Thank you!  That worked perfectly.  

Badge +2

Working with O365 as well, thanks for the aid @shirin .

Reply