Tutorial: Search a Text Entry Field for Keywords or Phrases

Have you ever wanted to check if certain keywords or phrases have been typed into a Storyline text entry field? 

Maybe you want to count how many important terms have been included in a learner's response to ensure that they have a solid grasp on the subject matter? Or perhaps you want to check if they've used a particular phrase as part of their response?

In this tutorial, we will look at how to use JavaScript to analyse the text entered into a Storyline text entry field and count how many times particular keywords are mentioned.

Click here to see a demo and here to download the source file.

Let me know if you have any questions.

11 Replies
Susan Chen

Hi Mat,

I have a text entry exercise where the different response will be displayed based on what a user will type. In other words, the text entry will be validated. I want to validate if the entry contains certain keywords I am looking for, regardless the cases (Storyline 360 has this function), or spaces before or after the keywords, or plural forms, or other synonyms, or a full sentence, as long as it contains two keywords.

I do not know much about JavaScript. Looked at your demo and source file, don't think your solution solves my need. Can you or anyone help?

Matthew Bibby

Susan, sorry for not responding to you earlier. While we may need to modify the code a bit, this should do what you wanted to do (last year!). Yell out if you still need help with this.

Kelly, you can use this code to do that. In the demo, you can see that I've set the keywords to be onetwoskip a few - but these could just as easily be ownownerownership.

Would you need to track how many times these words are used? Or just check if the words have been used at least once?

Matthew Bibby

Kelly, is this the kind of thing you had in mind?

Here is a demo.

Here is the source file.

Here is the JavaScript that is triggered when Submit is pressed:

var player = GetPlayer();
var text = player.GetVar('TextEntry');
var score = 0;
var pos = text.toLowerCase().indexOf('own');
while (pos !== -1) {
score++;
pos = text.toLowerCase().indexOf('own', pos + 1);
}
player.SetVar("Points",score);

You'll need two variables in your course for this to work. One called TextEntry - which is a text variable and is attached to the text entry field. And another called Points (which is a number variable and will show how many times the word own is used).

Hope this all makes sense!

Kelly Doane

Hi Matthew,
Thank you so much for your response. I have watched your tutorial - and it may be very close to what I'm looking for. In stead of counting use of a word however I'd like something to be triggered on a future slide.

Here is what I'd really like to have happen: I'd like the learner to type a word in a textenrtry box on slide 1, say 'own'. Then on slide 2 I'd like the system to recognize the typed word and if the typed word contains any part of a word, say 'ownership' a trigger fires.

For context, I'm writing a script that will ask the learner to text enter one word characteristics of a good csr experience. They might enter things like: "knowledgeable", "courteous", "timely". If they typed any part of the characteristics words I have on the review slide (slide 2), I want those words to be underlined.

Learner text entry slide 1: Informative
(trigger: if text entry contains 'inform' change state of 'Kept me informed' to Normal)
Review slide 2: Kept me informed

Really looking forward to a 'contains' option within Triggers!

Thanks again for your help,
Kelly

Kelly Doane

Ok, I think I have this working for one word scenario. var Points does not = 0 trigger slide var. But I think I'm looking less for the number of times something occurs and more for a value/contains statement. 

If slide 1 textentry1 contains 'own' show layer 4 on Slide 2. 

If slide 1 textentry2 contains 'polite' show layer 6 on Slide 2

If this can't be done using separate textentry boxes, I would consider using one textentry for all. 

Sorry this is so difficult for me to explain. 

Thanks,

Kelly 

Matthew Bibby

Hi Kelly,

See if this does the trick.

In this version, there is one text entry field that looks for the two words and a third phrase.

Here is the source file.

Here is the JavaScript used. Note that this assumes you have a Text variable called TextEntry and three True/False variables called Own Polite and SomethingElse

// Establish communication with Storyline
var player = GetPlayer();

// Get variables from Storyline
var text = player.GetVar('TextEntry'); // This is used by the text entry field
var own = player.GetVar('Own');
var polite = player.GetVar('Polite');
var somethingElse = player.GetVar('SomethingElse');

// Search text for the term 'own' and update variable if found
var pos = text.toLowerCase().indexOf('own');
while (pos !== -1) {
own = true;
pos = text.toLowerCase().indexOf('own', pos + 1);
}

// Search text for the term 'polite' and update variable if found
pos = text.toLowerCase().indexOf('polite');
while (pos !== -1) {
polite = true;
pos = text.toLowerCase().indexOf('polite', pos + 1);
}

// Search text for the phrase 'something else' and update variable if found
pos = text.toLowerCase().indexOf('something else');
while (pos !== -1) {
somethingElse = true;
pos= text.toLowerCase().indexOf('somethingElse', pos + 1);
}

// Send varialbes to Storyline
player.SetVar("Own",own);
player.SetVar("Polite",polite);
player.SetVar("SomethingElse",somethingElse);

And here is another version. In this one, there are separate text entry fields for each word of interest. 

Here is the source file

And here is the JavaScript used for the first text entry field. Note that this assumes you have a Text variable called TextEntry1 and a True/False variable called Own

// Establish communication with Storyline
var player = GetPlayer();

// Get variables from Storyline
var text = player.GetVar('TextEntry1'); // This is used by the text entry field
var own = player.GetVar('Own');

// Search text for the term 'own' and update variable if found
var pos = text.toLowerCase().indexOf('own');
while (pos !== -1) {
own = true;
pos = text.toLowerCase().indexOf('own', pos + 1);
}

// Send varialbes to Storyline
player.SetVar("Own",own);

And here is the JavaScript used for the second text entry field. Note that this assumes you have a Text variable called TextEntry2 and a True/False variable called Polite

// Establish communication with Storyline
var player = GetPlayer();

// Get variables from Storyline
var text = player.GetVar('TextEntry2'); // This is used by the text entry field
var polite = player.GetVar('Polite');

// Search text for the term 'polite' and update variable if found
pos = text.toLowerCase().indexOf('polite');
while (pos !== -1) {
polite = true;
pos = text.toLowerCase().indexOf('polite', pos + 1);
}

// Send varialbes to Storyline
player.SetVar("Polite",polite);

Hope that helps!