Jennifer
Forms Fledgling

Formatting text using JavaScript in NintexForms

Jump to solution

I have a textfield in Nintex Forms to which I assigned the following JavaScript variable name:

 

 

varAirplaneContent

This textfield has content inside which has a wrong format:

wrong format.PNG

 

I wanted to use the NintexForm user defined JavaScript to edit the content of this field.

 

I tried using 

var text = 'Reservation - Airplane Content';
var json = JSON.parse(text);
var count = json.length;
var flightDataSplitted;
for (var i=0; i < count; i++){ flightDataSplitted = json[i].Title.split(","); console.log(flightDataSplitted); } function testFunction(){
console.log("testFunction called!"); NWF$('#'+varAirplaneContent).val(flightDataSplitted); } NWF$(document).ready(function(){ testFunction(); });

but it doesn't work:

not working.PNG

The content in the field is still the same. What am I doing wrong?

0 Kudos
Reply
15 Replies
Tarf
Design Canvas Artiste

Re: Formatting text using JavaScript in NintexForms

Jump to solution

Hi,

 

in your for-loop you are splitting your values at "," - why? There is no "," in your variable you could split by.

This makes it so that the value returned is an array, which I think can not be written to the single line text field without using ".toString()".

Also I think the script provided can not work, because you are operating on the text "Reservation - Airplane Content" and not the form data itself (even though your screenshots show different).

 

What do you want the text field in the end to look like?

I came up with this js, which puts the destinations next to each other separated by " // " maybe you can adapt it to your needs?

//text should of course be some data via form-variable, not hardcoded
var text = '[{ "Id": 22, "Title": "München - Stockholm 31.01.2017 15:00" },{ "Id": 23, "Title": "Stockholm - München 01.02.2017 18:00" }]'; //turn text into an object we can work with
var parsedData = JSON.parse(text);
//holds the formatted text var flightData = ""; for (var i = 0; i < parsedData.length; i++) { console.log(parsedData[i]); console.log(parsedData[i].Title); flightData += parsedData[i].Title; //if it is not the last iteration if (i < parsedData.length - 1) { flightData += " // "; } } console.log(flightData); //-> München - Stockholm 31.01.2017 15:00 // Stockholm - München 01.02.2017 18:00

This part of your script looks correc to me. Have you tried setting it to some hard coded string to see if it works? I think in your script flightDataSplitted is an array, so that may cause the problem 

NWF$('#'+varAirplaneContent).val(flightDataSplitted);
Reply
Jennifer
Forms Fledgling

Re: Formatting text using JavaScript in NintexForms

Jump to solution

Thank you very much for your reply Tarf!

 

It looks perfectly fine on JSFiddle, but the problem is like you said 

NWF$('#'+varAirplaneContent).val(flightDataSplitted);

Even if I set the .val to a hardcoded String like "Test" it doesn't replace the content and the console doesn't show an error either.

I think the problem is the JSON format, but I don't know how the .val(); needs to be formatted.

 

related.PNG

 

To get the input I also tried

 

console.log("varAirplaneContent: " + NWF$("[data-controlname='ReservationAirplaneContent'] [c2516f79-978c-435e-9a74-439b82ed7276][name]").val());

but it says 

varAirplaneContent: undefined

 

Do you have any idea how to fix this? Thanks again! 🙂

0 Kudos
Reply
Tarf
Design Canvas Artiste

Re: Formatting text using JavaScript in NintexForms

Jump to solution

What does you console show when you execute the code?

For example:2020-04-21 12_27_43-f - New Item.png

What type of column is "Reservation - Airplane Content"? Single Line of Text?

Reply
Jennifer
Forms Fledgling

Re: Formatting text using JavaScript in NintexForms

Jump to solution

Multiple lines of text.

This is what the console shows:

 

it shows this.PNG

0 Kudos
Reply
Tarf
Design Canvas Artiste

Re: Formatting text using JavaScript in NintexForms

Jump to solution

Is it multiple lines with rich or just plain text?

Plain text can be changed using NWF$("#" + myVariable).val("My Text").

Please make sure to use the correct syntax for the call. In your screenshot you are providing text instead of the variable and forgot the leading "#".

Reply
Jennifer
Forms Fledgling

Re: Formatting text using JavaScript in NintexForms

Jump to solution

My bad, here's the correct syntax:

 

correct syntax.PNG

It's plain text.

0 Kudos
Reply
Tarf
Design Canvas Artiste

Re: Formatting text using JavaScript in NintexForms

Jump to solution

please make sure to call it like so "#" + your_variable_name.

NWF$("#" + varAirplaneContent).val("Test")

In this case, it is all text and therefore no field is found.

Is your field disabled or has any other special configuration?

0 Kudos
Reply
Jennifer
Forms Fledgling

Re: Formatting text using JavaScript in NintexForms

Jump to solution

Oh, didn't think that would make a difference, but apparently it does:

 

difference.PNG

 

I looked, but the field is not disabled and doesn't have any other special configuration..

0 Kudos
Reply
Tarf
Design Canvas Artiste

Re: Formatting text using JavaScript in NintexForms

Jump to solution

Thats odd, your selector does not seem to find the control to set.

Usually you should see results (for example the length property for number of matches)

2020-04-21 13_30_07-f - New Item.png

Is your form loaded in view mode?

I am able to reproduce this behaviour in view mode but not in edit or new mode

0 Kudos
Reply