Remove/Ignore spaces at the end of multiple text entry fields - Javascript question

Hi
I wonder if any Javascript wizards can help?
Following this thread:
https://community.articulate.com/discussions/articulate-storyline/remove-ignore-spaces-at-the-end-of-text-entry-fields

var p = GetPlayer();
var textentry = p.GetVar("TextEntry");
var newTextentry = textentry.replace(/^\s+|\s+$/g, '');
p.SetVar("TextEntry",newTextentry);


I managed to copy the script to remove spaces from a text entry field called "TextEntry".

However, I would like to be able to iterate this through multiple text entry fields, which get names like "TextEntry1", "TextEntry2", etc etc.

Presumably I need some kind of loop, which appends a number to "textentry", "TextEntry", "newTextentry", and cycles through it until the nth iteration, but I don't know how to code this, and can't find an answer anywhere.

I hope this makes sense, and apologize if I've posted in the wrong place.

4 Replies
Paul Read

Hi
Yes, I got there in the end...!

This script will run through the variables TextEntryX to TextEntryY (you can specify the first and last in the series by changing X and Y below to numbers) and replace the input in each of those variables with a "cleaned up" version with spaces and apostrophes etc removed. 

 

var p = GetPlayer();

for(var i = X; i < Y; i++) {
var textentry = p.GetVar("TextEntry" + i);
var newTextentry = textentry.replace(/^\s+|\s+$|\'/g, '');
p.SetVar("TextEntry" + i,newTextentry);
}

Javier Tapia

Hi, thanks for sharing your code. I was trying to make it work in one of my projects, I didn't at the begin but at the end with some modifications it did.

What I did to make it work: for the first slide I used the following code (the first slide with text entry):

var p = GetPlayer();

var p = GetPlayer();

var textentry = p.GetVar("TextEntry");

var newTextentry = textentry.replace(/^\s+|\s+$/g, '');

p.SetVar("TextEntry",newTextentry);

 

Then for the rest I used this one:

var p = GetPlayer();

for(var i = 1; i < Y; i++) {

var textentry = p.GetVar("TextEntry" + i);

var newTextentry = textentry.replace(/^\s+|\s+$/g, '');

p.SetVar("TextEntry" + i,newTextentry);

}

 

-Y- should be the last text entry you have plus 1.

 

Make sure your triguer is before the submit interaction trigger.