Can't read a text value that gets updated by Javascript in my Nintex Form.
Hi, I have a nintex form that uses custom javascript to get values from a repeating section collection, adds them up, then places the value in a single line text field. Everything works as needed until I need to read that updated single line text field that gets changed by my javascript.
Here are some details of how I update the single line text box:
I name the Client ID JavaScript variable name as ‘totalDuration’ like so
Then in my custom javascript I update the textbox like this:
var showDiff = NWF$('#'+totalDuration); showDiff.val(totalDiff);
This successfully updates the textbox. The problem is when I refer to the text box by its name “txt_totalDuration” in a rule, a variable, or calculated field, it is not able to read the updated value.
Does anyone have an idea how I can fix this? The line of js that updates the value is in a async function. I have also tried updating a calculated field instead of a textbox and that does not work.
This is in Nintex Forms for Sharepoint 2019. Version 5.2.4.40
Page 1 / 1
It’s likely because you’re not triggering any event on the Input after you’ve updated it.
Let’s say I have a form that looks like:
it has an input field in a RS section, an input outside of it, and a Calculated Control that will take the value of the Total Duration control and add 10 to it:
In the Custom Javascript of the form I have some generic code that will grab a bunch of inputs from a Repeating Section and update the value of a Single Line Text control somewhere outside of the Repeating Section:
NWF$("sdata-controlname='RSSection']").on("change", function(event){ var repeatingRows = NWF$(event.currentTarget).find(".nf-repeater-row:not" + "(.nf-repeater-row-hidden)"); var inputs = repeatingRows.find("idata-controlname='control_RS_Value'] input.nf-associated-control");
var totalValue = 0; inputs.each(function(index, input){ var inputValue = NWF$(input).val();
if (inputValue) { inputValue = parseInt(inputValue, 10);
if (!Number.isNaN(inputValue)) { totalValue += inputValue; } } });
/* this is the important line where I update the Input */ NWF$("});
(NOTE: as I comment in the code the important line that updates the Total Duration control is located near the bottom of the code. DO NOT use the code around in production! this is only used as an example and is not really a great way of getting a bunch of Repeating Section Control values!)
When I load the form in a preview this is what I see:
Let’s add a value to the RS Value:
Hmm… It updated the Input but not the Calculated Control!
To fix this I’ll need to add a small addition to the code that I’m using to update the Total Duration Control. Instead of that last line being:
NWF$("sdata-controlname='RSSection']").on("change", function(event){ var repeatingRows = NWF$(event.currentTarget).find(".nf-repeater-row:not" + "(.nf-repeater-row-hidden)"); var inputs = repeatingRows.find(" data-controlname='control_RS_Value'] input.nf-associated-control");
var totalValue = 0; inputs.each(function(index, input){ var inputValue = NWF$(input).val();
if (inputValue) { inputValue = parseInt(inputValue, 10);
if (!Number.isNaN(inputValue)) { totalValue += inputValue; } } });
/* this is the important line where I update the Input */ NWF$("ddata-controlname='control_totalDuration'] input.nf-associated-control").val(totalValue).trigger("blur"); });
The Results:
Now everything works.
All Nintex Form Controls have their way of informing the Calculated Values, Rules, and other Dependencies that they have been updated, but not all controls use the same event! Some controls use “change” instead of “blur”
The following code snippet is directly from Nintex’s code that assigns all of the event handlers to each of the Controls by the type that they are. You can see that, for instance, a select element triggers calculated updates by way of the “change” event.
AttachOnChangeEvents: function (formFillerDivCurrent) { formFillerDivCurrent.on('focus', 'input:text', function (event) { NWF$(this).data("OldValue", this.value); });
formFillerDivCurrent.on('focus', 'textarea', function (event) { NWF$(this).data("OldValue", this.value); });
// Attach event to trigger calucations (dynamic values) on text boxes using the "blur" event formFillerDivCurrent.on('blur', 'input:text', function (event) { if (NWF$(this).data("OldValue") !== this.value) { NWF.FormFiller.Functions.ResetCircularReferenceRelatedVariables(); NWF.FormFiller.Functions.ProcessOnChange(NWF$(this)); } });
// Attach event to trigger calucations (dynamic values) on option buttons using the "change" event formFillerDivCurrent.on('change', 'input:radio', function (event) { // Reset all variables used to detect circular references NWF.FormFiller.Functions.ResetCircularReferenceRelatedVariables();
// Attach event to trigger calucations (dynamic values) on option buttons using the "change" event formFillerDivCurrent.on('change', 'input:checkbox', function (event) { // Reset all variables used to detect circular references NWF.FormFiller.Functions.ResetCircularReferenceRelatedVariables();
// Attach event to trigger calucations (dynamic values) on multi line text boxes using the "blur" event formFillerDivCurrent.on('input propertychange blur change', 'textarea', function (event) { if (NWF$(this).data("OldValue") !== this.value) { // Reset all variables used to detect circular references NWF.FormFiller.Functions.ResetCircularReferenceRelatedVariables();
// Attach event to trigger calucations (dynamic values) on drop down list using the "change" event formFillerDivCurrent.on('change', 'select', function (event) { // Reset all variables used to detect circular references NWF.FormFiller.Functions.ResetCircularReferenceRelatedVariables();
I hope this helps to shine some light on the issue, and solves your problem.
Yes, this is extremely helpful. Thank you for the clear explanation and example. It turns out all I was missing was that :
.trigger(‘blur’)
function.
I am curious, is there any documentation for using javascript with nintex forms?
I am familiar with using javascript to work with the DOM in a typical HTML file and I’ve realized that using javascript with a nintex form is a whole different world, and that is understandably so. I spent some time looking online for some proper documentation for all of the nintex forms javascript functions and the basics but had no luck. Do you know of any type of documentation that could be helpful?
There really isn’t a definitive resource that goes in depth with basic concepts for using JS in Nintex Forms. Though I have a rough draft of something that I started yester-yester-yester-year, when the community blog post feature was removed from these forums 2 updates ago I scrapped the idea as it would have been too cumbersome of a post to make in this Question Only format / Classification.
That said I did recently purchase a blog-space that’s being setup to post about some of the odds and ends of SharePoint / Nintex stuffs. It would be a good place to put such a resource, so once I am up and running I will try to give you a heads-up if you’re still interested.
Until then if you have any particular questions, feel free to ask me in PM or tag me in a new Forum Topic. However my time here is coming to an end.
Gotcha. Well I really appreciate the help and you taking the time to reply here. I would definitely be interested in that blog space as I am just starting to work with nintex & sharepoint. It is too bad Nintex doesn’t want to support javascript … before I made this original question, I made a case to nintex support and they said they simply don’t support javascript and could not help me, which is also why a blog space, such as the one you are setting up, would be extremely helpful to folks like me.