How to transfer text to a new line?

Dec 20, 2018

I have come across a problem with text entry field boxes and the TextEntry variables. I've got a task to make a slide (storyline 3) where employee should write letter using text entry and then send this letter to curator of the project by email.

The problem: when I write letter and transfer text to a new line, program perceives it as a single line.

for example:

TO:
FROM:
SUBJECT:

Program converts this text to the following format: TO:FROM:SUBJECT:

Then I set variable equal to the typed value and execute JavaScript that creates Outlook message and moves this variable as the mail body.

How to transfer typing text to a new line  in text entry field?

I use  j a v a s c r i p t  to create new outlook message with text should be sent:

...
var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start;
if (myVar != '') {win=window.open(mailto_link,'emailWin');} else {alert('Field is empty!');}

There is an example in attachment.

Hope someone's help.
Thanks,
Dmitriy

6 Replies
Dmitriy Sergeevskiy

Hi, Daniel.

This works in Articulate when the handler processes the  text in code. 

for example, if I write some code:
var body_start='Hello, Name.\n How are you?'

It won't work. Result will be: Hello, Name. How are you?

In variables, we can not move text to another line. This should be done with the help of an additional function
If you need to move your text the next line, you should write:

var newLine=escape('\n');
var body_start='Hello, Name.' + newLine + 'How are you?'

Result will be: 

Hello, Name.
How are you?

But this method works when you have fixed text in code to be shown on the screen.

I have a problem when project is already published and I'm trying to send my letter using text entry field, where I write my text - program does not accept line breaks and the email formed without breaks in a single line ((((

Dmitriy Sergeevskiy

The issue is resolved with help of JS.
If you face this problem, here is the solution:

myVar - name of variable of Articulate text entry field

var player  = GetPlayer();
var myVar = player.GetVar("myVar");
var arrMy = myVar.split("\n");
var newLine=escape("\n");

// + some variables for mail: subject, body, body_start, body_end

for (i = 0; i < arrMy.length; i++) {

  body_start += arrMy[i];

  body_start += newLine;
}

var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start+body_end;

if (myVar != '') {win=window.open(mailto_link,'emailWin');}

else {alert('Field is empty');}

 

Good luck! :)

Stefan Gottfried

Hi Dimitriy,

thank you for this post! I tried to adjust it to my needs and i get a semi-nice outcome. My code looks like this: 

 

//here i declare my variables:

var player = GetPlayer();
const email = "xxx@xxx.com";
const subject = "my notes to this course";
let Notizen1JS = player.GetVar("TexteingabeNotizen1");

//most of this is your code:
var arrMy = Notizen1JS.split("\n");
var newLine = encodeURI("\n");

for (i = 0; i < arrMy.length; i++) {
Notizen1JS += arrMy[i];
Notizen1JS += newLine;
};

const mailto_link = 'mailto:' + email + '?subject=' + subject + '&body=' + Notizen1JS + Notizen2JS;

//i prefer this line to send an email. otherwise the browser starts a new tab
window.location.href = mailto_link;

my problem: IThe user enters the "TexteingabeNotizen1" in storyline as this:

test1, test 2

 

test line 3

 

The e-Mail looks like this:

test1, test 2test1, test 2test line3

 

test line 3

 

What do I miss?

Stefan Gottfried

I got a working solution for two text entry fields, woohoo! I am happy to share my code, that was produced with help from this and other forums and nice guys:

//Connect SL with Javascript
var player = GetPlayer();

//e-mail recipient and subject
const email = " ";
const subject = "my notes to this course";

//Get the text entry variables from SL
let Notizen1JS = player.GetVar("TexteingabeNotizen1");
let Notizen2JS = player.GetVar("TexteingabeNotizen2");

//replace all new lines from SL with new lines in JavaScript
Notizen1JS = Notizen1JS.replace(/(?:\r\n|\r|\n)/g, '%0A');
Notizen2JS = Notizen2JS.replace(/(?:\r\n|\r|\n)/g, '%0A');

//set up the e-mail with two new lines between the first notes and the second notes
var mailto_link = 'mailto:' + email + '?subject=' + subject + '&body=' + Notizen1JS + '%0A' + '%0A' + Notizen2JS;

//open a new e-mail
window.location.href = mailto_link;

This discussion is closed. You can start a new discussion or contact Articulate Support.