Javascript not triggering
Jan 09, 2019
I'm trying to eliminate having to put in double entries for every possible answer, one without a trailing space and one with.
Whatever is entered into the textentry box, I want it converted to all CAPS and then I want all leading and trailing spaces removed. Acceptable answers would be:
I am
Me
Myself
You
Yourself
You are
Here is my code, I've tried adding it to the textentry loses focus event and to the presses submit button event.
var p = GetPlayer();
var textentry = p.GetVar("TextEntry");
var newtextentry = textentry.toUpperCase();
p.SetVar("TextEntry",newtextentry.replace(/^\s+|\s+$/g, ')');
Nothing seems to be triggering at all. I should be able to type in "i am " with a bunch of extra spaces and it changes to "I AM" with no extra spaces.
15 Replies
Have you opened the developer console to see what errors you get?
Didn't know that was a thing. Saw this in there:
Uncaught SyntaxError: missing ) after argument list
2app.min.js:3 Uncaught TypeError: Cannot read property 'setAttribute' of null
at e.determineChildMouseBehavior (app.min.js:3)
at e.whenHtmlReady (app.min.js:3)
story_html5.html?lms=1:1 Uncaught (in promise) DOMException
2app.min.js:3 actionator::exeJavaScript - this.execUserJs is not a function
(anonymous) @ app.min.js:3
story_html5.html?lms=1:1 Uncaught (in promise) DOMException
I've given up on the CAPS thing, now just trying to get a TRIM script to trigger and run, still not doing anything. I'm stuck.
var p = GetPlayer();
var textentry = p.GetVar("TextEntry3");
var newTextentry = textentry.replace(/^\s+|\s+$/g, '');
p.SetVar("TextEntry3",Textentry);
taken from this page:
https://community.articulate.com/discussions/articulate-storyline/remove-ignore-spaces-at-the-end-of-text-entry-fields
is it not "newTextentry" that you want to send back to SL? also the case is wrong on Textentry if that is the variable you want to send back, sorry not checked the other syntax
OK, it appears it actually WAS working, but only when the SUBMIT button was pressed. I needed to add a duplicate trigger for the ENTER key and now it seems to work for all entries.
Phew.... and yes, you were correct, I pasted the snippet above from the original site, in my project it was newTextentry. ;)
Hi Jerry, I am also trying to have spaces ignored with that javascript- can you please tell me how you got it to work? I downloaded your story file but it says incorrect when I enter spaces (I assume this was the file before you fixed it!). Thanks!!
Hi Jerry, I am also trying to have spaces ignored with that javascript- can you please tell me how you got it to work? I downloaded your story file but it says incorrect when I enter spaces (I assume this was the file before you fixed it!). Thanks!!
This works for me but only after publishing. I've added triggers to execute the JS on pressing the ENTER key or pressing the SUBMIT key.
This only removes spaces at the front or back of the text string.
var p = GetPlayer();
var textentry = p.GetVar("TextEntry");
var newTextentry = textentry.replace(/^\s+|\s+$/g, '');
p.SetVar("TextEntry",newTextentry);
https://360.articulate.com/review/content/a180768a-9129-4b86-8d31-70e3b80d52ae/review
Hello everyone,
I have the same problem although there is no such thing as a Trigger with JS on my script, could anybody help ?
Thanks in advance
The trigger with JS is listed in the triggers as "Execute Javascript", it's listed in the trigger "ACTION" section called "MORE".
Hello, is it an answer for me ?
Yes, Julie. You indicated you didn't know where/how to put javascript to run on a triggered moment. When I look in storyling the "Execute Javascript" trigger action is listed in the MORE section.
MORE
Adjust Variable
Pause Timeline
Resume Timeline
Jump to URL/File
Send email to
Execute Javascript
Hello,
Does anyone know of a working script for removing leading and trailing spaces in text boxes? The JS script above does not seem to work for me, perhpas because of the latest SL updates....
Thanks,
Robert
Oh it actually works, my bad. Is just that my text variable was named differently, TextEntry25 instead of TextEntry.
var p = GetPlayer();
var textentry = p.GetVar("TextEntry25");
var newTextentry = textentry.replace(/^\s+|\s+$/g, '');
p.SetVar("TextEntry25",newTextentry);
Cheers
Hi, I'm working with Storyline 3. Try to do this in order to solving the problem of the spaces before and after the text, but this dosent work for me when pressing the ENTER key... ¿Can anyone help me, please?
Thanks