Set List Lookup value using Javascript

  • 3 April 2018
  • 7 replies
  • 15 views

Badge +2

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

7 replies

Userlevel 5
Badge +14

have a look eg. on this post

https://community.nintex.com/message/75000-re-javascript-to-set-value-in-lookup-list-control?commentID=75000#comment-601… 

Badge +2

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!
Userlevel 2
Badge +11

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 wink.png

Badge +2

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

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

Any other idea? happy.png

Userlevel 2
Badge +11

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','');

Badge

Hi @chris0204 ,

You can do this to, it's easier

//YourControl class must have the same name as your control 
var selectControl = NWF$('.' + 'YourControl select'); //YourIndex has to be a number selectControl[0].selectedIndex = YourIndex;

Arthis

Badge

Don't forget to add

//Raise change event on select control
NWF$('.YourControl select').trigger("change");

Reply