Skip to main content
Nintex Community Menu Bar

How to set default for a lookup column using jquery in nintex

  • March 23, 2018
  • 13 replies
  • 83 views

Forum|alt.badge.img+2

I was wondering anyone could shed some lights on this.

 

I want to set default to EN ENGLISH on my list lookup (Language) as below but its editable.

 

 

 

JQuery

NWF$(document).ready(function() {   
 
  //setLookupField(DetailLanguage, 'EN ENGLISH');
}); NWF.FormFiller.Events.RegisterAfterReady(function () {
 
 if (DetailLanguage == "Please select a value..."){
 
 setLookupField(DetailLanguage, 'EN ENGLISH');
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},1000);
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},2000);
 }else{
  setLookupField(DetailLanguage, 'EN ENGLISH');
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},1000);
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},2000);
  
 }});
function setLookupField(lookupField, lookupValue) {
 var lookupID = lookupField.substring(0, lookupField.length-4);
 var selectField = NWF$("#" + lookupID + " > option");
 
 for (var i = 0; i < selectField.length; i++){
  var option = selectField;
  if(option.text == lookupValue){
   NWF$("#" + lookupID).val(option.value);
   NWF$("#" + lookupField).val(option.getAttribute("data-nfchoicevalue"));
   NWF$("#" + lookupField).trigger("change");
   NWF$("#" + lookupField).trigger("blur");
   break;
  }
 }
}

13 replies

Forum|alt.badge.img+9
  • Scholar
  • March 26, 2018

In O365, you can use this script to update a list lookup value:

var valueText = "EN ENGLISH";

var yourControl = NWF$("#"+yourControlId);

var value = yourControl.find("option[title='" + valueText + "']").attr("value");

yourControl.val(value);

yourControl.parent().find("input").val(valueText + "#--#" + value);

NWF$(NWF$(this).parent().find("input")[1]).val(value);

Hope this works


Forum|alt.badge.img+2
  • Author
  • March 28, 2018

May I know what is [1] referring to?


Forum|alt.badge.img+2
  • Author
  • March 28, 2018

where do i put this?


Forum|alt.badge.img+2
  • Author
  • March 28, 2018

Hi Caroline Jung

it's working do you mind explaining these lines?

var valueText = "EN ENGLISH"; // default value which i needed

var yourControl = NWF$("#"+yourControlId); // jquery name of the above item

var value = yourControl.find("option[title='" + valueText + "']").attr("value"); // set var value to en english in option list lookup

yourControl.val(value); //set en english to yourControl

yourControl.parent().find("input").val(valueText + "#--#" + value);  //?

NWF$(NWF$(this).parent().find("input")[1]).val(value);  //? why the [1] ? is it the list item id of the particular item ?


Forum|alt.badge.img+2
  • Author
  • March 29, 2018

Caroline Jung

Hi,

I dont know why suddenly the default to en english isnt working

its triggering from this line

yourControl.parent().find("input").val(valueText + "#--#" + value);  


Forum|alt.badge.img+4

If you know the ID of your element, you can do this :

var control = ko.dataFor(NWF$("#" + varMyControlID)[0]);
control.initialValue  = "X";

where X is the ID of your element in the list


Forum|alt.badge.img+9
  • Scholar
  • April 5, 2018

Hi, 

Sorry for the delay, here are explainations:

var valueText = "EN ENGLISH"; // Setting the title of the option to select in the list lookup field

var yourControl = NWF$("#"+yourControlId); // Getting the list lookup control on the form

var value = yourControl.find("option[title='" + valueText + "']").attr("value"); // Getting the value of the option to select in the list lookup

yourControl.val(value); // Setting the value of the list lookup control

yourControl.parent().find("input").val(valueText + "#--#" + value);  // As setting the value of the list lookup control is not enough to be taken into account by the Nintex Forms, another value has to be set:it's the value of an hidden input which should have a specific format

NWF$(NWF$(this).parent().find("input")[1]).val(value); // This is a mistake: you don't need to add this line

Don't hesitate to ask if it's not clear


Forum|alt.badge.img+9
  • Scholar
  • April 5, 2018

In the list lookup, does the option with this title still exist "EN ENGLISH"?

It doesn't seem to find the value corresponding to the title "EN ENGLISH".

If you're executing this script when the form loads, I advice you to use this script to be sure that it's executed when the list lookup has been loaded (maybe your script is executing when the list lookup is not loaded and doesn't have any option):

NWF$(document).ready(function() {

setDefaultValue();

});

 

function setDefaultValue(){

var valueText = "EN ENGLISH";

var yourControl = NWF$("#"+yourControlId);

if(yourControl.parent().find(".nf-lookup-loading").length > 0 && yourControl.parent().find(".nf-lookup-loading")[0].style.display != "none") {

setTimeout(setDefaultValue,200);

} else {

var value = yourControl.find("option[title='" + valueText + "']").attr("value");

yourControl.val(value);

yourControl.parent().find("input").val(valueText + "#--#" + value);

}

}

Hope this helps


Forum|alt.badge.img+9
  • Scholar
  • April 5, 2018

Thanks a lot for this, it works really great


Forum|alt.badge.img+7
  • Novice
  • July 10, 2018

What does ko.dataFor refer to? The knockout.js library? How does this work or is enabled for use with Nintex forms?


Forum|alt.badge.img+4

Yes it is knockout.js and it is loaded by Nintex Forms

They use it for lookup list field


Forum|alt.badge.img+7
  • Novice
  • July 18, 2018

Is the use of knockout.js documented anywhere within Nintex help and/or encouraged for use? Also, if Nintex decide to change to a different javascript library, then anything using knockout would cease to function. So something to be wary of I guess?


Forum|alt.badge.img
  • June 11, 2019
worked perfectly for me.