Setting a form field value based on choice value with Javascript


Badge +3

Hi all,

I am looking (on the press of a button) in a Nintex form to set the value of a Task Status field based on the value/selection of a choice field control (set as Option buttons).

Here is the set up.

I have a choice control field (not connected to any SharePoint field) in my form.

I have set display format to Option Buttons (only one should be selected)

I have two choices, values:

Start Query
End Query

Render as buttons = No

I have a variable in StoreClientID in JavaScript variable set to QueryOutcome.

I have a SharePoint Column called Task Status. This has values of:

Not Started
In Progress
Query
Completed.

I want to test the if and else of the javascript function to see if the code is retrieving the value of the choice field variable 'QueryOutcome' so I am using alert at the moment to see if all is working. However, it is only returning Stop Query Alert even when I select Start Query option on the form in the choice control. It appears the value of the choice control is not getting passed/retrieved.

My form button is of type Save and Submit and I have the following code under advanced/Client Click:

Eventually I want something in place of the alert command to set Item Property: Task Status == 'Query'

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

var Query = NWF$('#' + QueryOutcome);

if(Query == "Start Query"){

alert('Start Query Selected')

} else {

alert('Stop Query Selected')

}

  ;});


4 replies

Badge +8

Good Morning Darren Jones​,

There's probably a couple questions here, but for starters you might update your if statement so that you're evaluating the selected option for your QueryOutcome control instead of the control itself

Using some jQuery, you can access this using either of the following (depending on whether you're trying to leverage the label or value:

var var1 = NWF$('#' + QueryOutcome + ' option:selected').val();

var var2 = NWF$('#' + QueryOutcome + ' option:selected').text();

You'll notice that you're still using the same syntax to access the JavaScript Client ID variable for your control but you're pulling into a local variable it's selected option's text or value. Mind giving this a try and see if you need any follow-up assistance once you've got your test query working?

Thanks!

Patrick

Userlevel 5
Badge +13

Hey Patrick, I tried your solution on a choice field that is displaying as radio buttons (but still only one selectable, not a multi-choice checkbox type) and I'm getting undefined when I do .val() and nothing when I do .text() . Any ideas? 

My code looks like this:

NWF$("#" + newOrExisting).change(function ()
{
    var newOrExistingSelectedItem = NWF$("#" + newOrExisting + ' option:selected').val();
    alert("new or existing selected : " + newOrExistingSelectedItem );
});

The popup box is happening, but nothing in the newOrExistingSelectedItem spot.

I want to use the value here to check checkboxes in the next field (which is a multi-select checkbox) for the user.

Userlevel 5
Badge +13

Hey Patrick, just wanted to let you know I was able to find the solution here: https://community.nintex.com/thread/13321-i-have-a-nintex-form-with-two-choice-controls-radio-buttons-and-three-yesno-co… 

The final working code was:

NWF$("#" + newOrExisting).change(function ()
{
     var newOrExistingSelectedItem = NWF$('#' + newOrExisting + ' :checked').val();
}
Badge +2

:checked worked for me.  Thanks!

Reply