cancel
Showing results for 
Search instead for 
Did you mean: 
Nintex Newbie

Nintex Forms - set Lookup value using javascript

Jump to solution

Hi guys,

 

I know this has been already talked about but I can´t seem to find the issue with my code.

 

I have the following function which takes a client id javascript variable (from nintex forms) and a lookup id and sets the control.

function setDropdownLookupValue(control, lookupId) {
  //If 0 is passed, use the SelectValueMask as option value to look for
  const option_value = lookupId > 0 ? lookupId : NWF.FormFiller.Functions.SelectValueMask;
  // Nintex selects a hidden input field when accessing the lookup via js variable
  const hiddenInput = NWF$("#" + control);
  // By replacing the _hid by nothing, the selector will give us the choice field
  const choiceId = control.replace("_hid", "");

  //"Unselect" each option in the dropdown
  NWF$(`#${choiceId} option`).attr("selected", false);

  //Store the correct choice field by value
  const choice = NWF$(`#${choiceId} option[value='${option_value}']`);
  //Select it
  choice.attr("selected", "selected");
  //Read data-nfchoicevalue which contains the value we want to set the hidden input to
  const hiddenInputValue = choice.attr("data-nfchoicevalue");
  hiddenInput.val(hiddenInputValue);
}

And I have a list which looks as follows:

Title : Id

Title Id
A 1
B 2
C 3
D 4
E 6

 

I then create a lookup column to that list and add it to Nintex Forms.

With my function, I can set the lookup field, until I want to set the lookup field again to the same value. So if I am running the following in Chrome Dev Tools

setDropdownLookupValue(lookup,1)

The value gets changed to the item with id 1 (A)

If I use the code to set the lookup to 2, the value gets changed to item with id 2 (B).

When I then want to reset it to 1, it changes to "Please select a value".

 

Spoiler

I could then set the lookup to any value which is not 1 or 2 (or other used ones), as the already "used" ones all point to "Please select a value".

If I click save, the value of A (id:1) is saved to the list item.

 

Anyone got an idea what I need to change to be able to select the same value again?

 

Thanks!

 

Nintex Forms Version: 

Nintex Forms 2013 ( 2.11.5.0)
 
Labels: (1)
0 Kudos
Reply
3 Replies
Highlighted
Nintex Newbie

Re: Nintex Forms - set Lookup value using javascript

Jump to solution

It looks like you over complicated things Smiley Wink
Just find the right option to select, and use "prop" and "change" (or blur) to push changes and trigger rules. You also do not have to work with the hidden control (_hid) :

NWF$('#' + control + ' option[value="MyValue"]').prop("selected", true).change()

"attr" is not really well supported.

View solution in original post

Reply
Highlighted
Nintex Newbie

Re: Nintex Forms - set Lookup value using javascript

Jump to solution

Thank you so much allan, it works!

 

Edit:

For completeness, I now use the following function to set lookup controls.

I tried without replacing "_hid" but that did not work.

function setDropdownLookupValue(control, lookupId) {
  //If 0 is passed, use the SelectValueMask as option value to look for
  var option_value =
    lookupId > 0 ? lookupId : NWF.FormFiller.Functions.SelectValueMask;
  // By replacing the _hid by nothing, the selector will give us the choice field
  var choiceId = control.replace("_hid", "");

  //Select the correct choice value and trigger change
  NWF$(`#${choiceId} option[value='${option_value}']`)
    .prop("selected", true)
    .change();
}
Reply
Highlighted
Nintex Newbie

Re: Nintex Forms - set Lookup value using javascript

Jump to solution
Glad that you solved your issue.
I never had to work with the _hid, but maybe I was doing something differently.
The only thing you were missing was the ".change()" I suppose.
0 Kudos
Reply