How to concatenate text fields conditionally
Oct 02, 2018
Hi,
I have a course in which the users will select 3 out of 7 behaviors to commit to over the next 30 days.
The results are then emailed to their supervisor using Javascript. The script I have below works, but because it includes all the possible variables, inserts extra semicolons between empty variables.
What I would really like would be to format the email so that the body looks like:
Hi,
Here are the behaviors I'm committed to practicing for the next 30 days:
commitment 1
commitment 2
commitment 3
Right now, I'm using semicolons to separate the commitments instead of line breaks because if the user selects behaviors 1, 5, and 7 there would be a bunch of line breaks between 1 and 5.
So... how do I get the variable "commitments" to concatenate only the populated commitment variables?
Below is the javascript I'm using now. Thank you in advance for your help!
=====
var player = GetPlayer();
var email = player.GetVar('email');
var commitment1 = player.GetVar('commitment1');
var commitment2 = player.GetVar('commitment2');
var commitment3 = player.GetVar('commitment3');
var commitment4 = player.GetVar('commitment4');
var commitment5 = player.GetVar('commitment5');
var commitment6 = player.GetVar('commitment6');
var commitment7 = player.GetVar('commitment7');
var commitments = commitment1 + '; ' + commitment2 + '; ' + commitment3 + '; ' + commitment4 + '; ' + commitment5 + '; ' + commitment6 + '; ' + commitment7;
var subject = 'My Commitments to Reducing Escalations Behaviors';
var emailBody = 'Hi,' + '\n' + '\n' + 'Here are the behaviors I am committed to practicing for the next 30 days: '+ commitments + '\n' + '\n' + 'Thank you,' + '\n' + '\n';
window.location.href='mailto:'+email+'?subject='+subject+'&body='+encodeURIComponent(emailBody);
2 Replies
Maybe this older thread here helps. It discussed making sure that a user selects exactly a certain number of choices and then create a list of these choices. If that approach works for you then you could simply your Javascript to only concatenating three variables, because they will never be empty.
Michael, thanks for the link to the discussion. Yes, that did help. I was able to do what I needed using triggers. (I did also figure out how to do it with JavaScript using a series of If/Else statements.
This discussion is closed. You can start a new discussion or contact Articulate Support.