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

Setting a default value for a dropdown

Sometimes I need a cross-site lookup (which is possible with the Nintex Lookup control) to select a value. My Filtering will always result just one value or the first one should fit. We need to set the first option from the list as selected. 

Lookup control settings (Advanced section):

  • Store Client ID in JavaScript variable
  • Name it. Example: varMyLookupField

Custom Javascript to Form Settings:

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

var myLookupDropdownControl = NWF$('#' + varMyLookupField);

myLookupDropdownControl.change(function(){

NWF$('#' + varMyLookupField).val('1')

});

} );

Labels: (3)
0 Kudos
Reply
6 Replies
Workflow Hero

Re: Setting a default value for a dropdown

Hi,

Do you just want to set it to the first option when its a new item? The script below is creating an onchange event so that it resets the choice to the first one every time it is changed?

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

var myLookupDropdownControl = NWF$('#' + varMyLookupField);

myLookupDropdownControl.change(function(){

NWF$('#' + varMyLookupField).val('1')

});

} );

If you just want to default it on a new form then the following script should work... thanks to Chit Siang Chew​ for the hints on this.

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

var isNewForm = window.location.pathname.indexOf("/NewForm.aspx") > -1;

if (isNewForm)
{
   NWF$('#' + varMyLookupField).val('1');
}
});

Jan

Accept as Solution Reply
Workflow Hero

Re: Setting a default value for a dropdown

I solved my issue with my solution, I just wanted to share it with the community. Thanks for the reply, I think it's gonna be useful for the future.

Accept as Solution Reply
Workflow Hero

Re: Setting a default value for a dropdown

Hi,

I have a similar demand.

We have a list of suppliers and when we enter a new one, the default country should be "Germany".

On another site we have a list with 200+ countries, area codes and dialing codes.

So it's obvious to make a lookup.

To set the default country when a new supplier is added I tried this code, but it didn't work.


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

var isNewForm = window.location.pathname.indexOf("/NewForm.aspx") > -1;

if (isNewForm)
{
NWF$('#' + varMyLookupField).val('1');
}
});

Jan

Than I combined the code to this:

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

var isNewForm = window.location.pathname.indexOf("/NewForm.aspx") > -1;

var myLookupDropdownControl = NWF$('#' + varCountry);

if (isNewForm){

myLookupDropdownControl.change(function(){

NWF$('#' + varCountry).val('1')

});

};

} );

This code works so far that it sets the default to "Germany" when i add a new supplier, but when I save it, the default country isn't saved in the list. When I manually choose the country it saves it.

Any assumption what went wrong?

Christian

Accept as Solution Reply
Workflow Hero

Re: Setting a default value for a dropdown

Hi Christian,

NWF$('#' + varCountry)[0].selectedIndex = 0 <- this is the first value in your dropdown list.

Hope it helps.

Accept as Solution Reply
Workflow Hero

Re: Setting a default value for a dropdown

Hi,

thanks for reply. Unfortunately it doesn't work.

My code looks like this now

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

var isNewForm = window.location.pathname.indexOf("/NewForm.aspx") > -1;

var myLookupDropdownControl = NWF$('#' + varCountry);

if (isNewForm){

myLookupDropdownControl.change(function(){

NWF$('#' + varCountry)[0].selectedIndex=49

});

};

} );

It does the same as my combined code. In the newform it preselects "Germany" (which index is 49) as country but this is not stored in the list.

The column in the list is "single line of text", does that make a difference?

In the control settings for the list lookup it says

- ID connected to "not connected"

- Text connected to "Country"

Do i have to change something here?

Accept as Solution Reply
Workflow Hero

Re: Setting a default value for a dropdown

Hi,

The problem with your solution is that you wired it to the Change event, so anything happens, it will select "germany". So they can'T change it to anything, it will always select that. (It that's what you want, maybe you should disable the control...)

Something like this maybe?

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

var isNewForm = window.location.pathname.indexOf("/NewForm.aspx") > -1;

if (isNewForm){

NWF$('#' + varCountry)[0].selectedIndex=49

};

} );

Cheers!

0 Kudos
Accept as Solution Reply