cancel
Showing results for 
Search instead for 
Did you mean: 
Workflow Hero

Set List Lookup value using Javascript

Jump to solution

Hello,

when I open a new Form I like to select a Element of a List Lookup based on a passed variable.

I can pass the variable to a Textfield in the form but for the List Lookup it isn't working.

I know that this topic has been discussed many times before but I don't get it work. 

My source code at "Custom Javascript":

function Autofill(myValue){
   NWF$("#"+varText_JS).val(myValue); //set Textfield works fine
   NWF$("#" + varLookup_JS + " > option[title='" + myValue + "']").prop("selected", true); //set Dropdown is not working
};

Thank you very much!

Christian

Edit:

Nintex Forms 2013 

Version: 2.11.2.0
Labels: (1)
0 Kudos
Reply
5 Replies
Workflow Hero

Re: Set List Lookup value using Javascript

Jump to solution
0 Kudos
Reply
Workflow Hero

Re: Set List Lookup value using Javascript 

Jump to solution

Thank you for your help. I used the following Code, but I can't get it work. 

NWF$(document).ready(function() {
var Test = '010'; //it is a value of the List
var secondCurrencyCtrl = NWF$("#" + varLookup_JS);
var selectCtrl2Id = varLookup_JS.replace('_hid','');
var selectElement = NWF$("#" + selectCtrl2Id); ;
NWF$("#" + selectCtrl2Id + " option:contains(" + Test +")").attr("selected", true);
});
Best regards!
0 Kudos
Reply
Workflow Hero

Re: Set List Lookup value using Javascript

Jump to solution

Hi Christian,

You missed one instruction at the end, which actually sets also the value itself. In your case it would look like:

NWF$("#" + selectCtrl2Id + " option:contains(" + Test +")").attr("selected", true); //your last line

secondCurrencyCtrl.val(Test);   // add this line after your last line.

BTW: the line "var selectElement = NWF$("#" + selectCtrl2Id); ; " has 1 semicolon too much

0 Kudos
Reply
Workflow Hero

Re: Set List Lookup value using Javascript 

Jump to solution

Thank you spending your time. But I can't get it work.  

Of course I used the debugger of my Browser to find the mistake . The function is called correctly. No Error message.

Any other idea?

0 Kudos
Reply
Workflow Hero

Re: Set List Lookup value using Javascript

Jump to solution

Try the following code, which I currently use successfully in one of my Nintex Forms to copy metadata from a queried list item:

   var secondCurrencyCtrl = NWF$("#" + varLookup_JS).parent().find('select.nf-client-control');
   var selectCtrl2Id;
   if (secondCurrencyCtrl && (secondCurrencyCtrl.length == 1)) {   // Try to get the proper Select element generically
      selectCtrl2Id = NWF$("#" + varLookup_JS).parent().find('select.nf-client-control')[0].id;
   } else { // Get proper Select element based on id
      selectCtrl2Id = secondCurrencyCtrl.replace('_hid','');
   }
   NWF$("#" + selectCtrl2Id + " option:contains(" + selectedItem.get_item('Currency').get_lookupValue() +")").attr("selected", true);

Here, "selectedItem" contains the List Lookup value for you currency to be copied. If you want to use a fixed value like Test in your original code, the last line then would look like this:

NWF$("#" + selectCtrl2Id + " option:contains(" + Test +")").attr("selected", true);

Because Nintex may change its list lookup control configuration in the future (as seemed to have happened around v2.9.x), and as such the control id suffix "_hid" may disappear again in a future update, I decided to determine the id of the 2nd lookup control more generically using the Then part of the above If. Otherwise I still will try the Else way to get the control's id.

NB: there was a small mistake in your original code:

var selectCtrl2Id = varLookup_JS.replace('_hid','');

should have been:

var selectCtrl2Id = secondCurrencyCtrl.replace('_hid','');

Reply