June 4, 2021
Why I Never Assign “Execute JavaScript” to Button Success Actions
Comments
(6)
June 4, 2021
Why I Never Assign “Execute JavaScript” to Button Success Actions
Newbie 1 posts
Followers: 0 people
(6)

If you’re like me, you’ve discovered that using JavaScript in your Captivate projects can help you build much more rich interactions with less work than relying on standard/advanced/shared actions. But if you have a JavaScript snippet that you want to run when the learner clicks a button, you might have noticed one big drawback with that approach:

When you assign Javascript to a button, you have to choose whether the “Continue Playing the Project” box should be checked or clear. The problem I’ve found with this selection is that checking it will re-start the timeline if it is currently paused, while clearing it will pause the timeline if it is playing when you click the button.

Depending on your project, one or the other of these choices may be fine, but I usually find that I want the button to not interfere with the playbar at all. So if it is playing when the button is clicked, keep playing. But if it’s paused, don’t restart it when the button is clicked. But there really isn’t a setting for that.

Interestingly enough, if you put an advanced action here, that is how it will behave. But it always seemed silly to me to have to potentially manage several different advanced actions that do nothing but call JavaScript. That’s when a buddy shared his solution that solved this once and for all.

Instead of assigning JavaScript to the button’s on success action, I leave that as No Action. Then I add an event handler using JavaScript in the slide’s On Enter action. So Set the On Enter action to Execute JavaScript (here you do want to have the “Continue Playing the Project” box checked), and use jQuery to add a click event handler to the button. Conveniently, you can use the Captivate name of the button as the target. Then add the javaScript you want the button to trigger in the callback function.

So if you have a button called Button_1, it would look like this:

If you’re not familiar with jQuery, this may look odd, but let’s break it down. The $ is just a shortcut for the jQuery library, so we will use it pretty much any time we want access to a DOM element. Inside the parentheses after the $ is just a CSS selector that finds the element with the ID of “Button_1” (# is the CSS selector for ID attribute).

The .click() function adds an “onclick” event handler to the element. The argument is a function. This function doesn’t need a name, so we just put function(){} inside the parentheses for the click() function.

All of your code goes in-between the curly braces in the anonymous function. To re-use it for different buttons, all you need to change is the button name in the selector, and of course the code in the callback function if you need it to do something different.

Hope this helps someone.

6 Comments
2022-08-19 18:05:51
2022-08-19 18:05:51

Thanks for the post. In fact, my experience tells me that I should always add an advanced action first, and execute JS from it. It gives me the possibility to add something if needed, not necessarily by adding the next JS lines but by choosing from available options. That’s how I approach the use of JS.

Like
2021-06-09 16:18:43
2021-06-09 16:18:43

Thanks!

Like
2021-06-07 11:59:40
2021-06-07 11:59:40

Thank you for this contribution. For those that do a lot of timeline work, this could be useful.

I find that I apply the Execute JavaScript action to buttons the majority of the time. On some occasions I will apply a simple action from the dropdown but it is not very often that my buttons perform a single thing. I also always uncheck the box for Continue Playing the Project.

This is due to the fact that I personally don’t use the timeline except to ensure my objects are stacked the way I need them to be. My objects are all set to 1 second default length and everything is event driven. Every slide is always on pause unless the learner makes the choice to move on.

So, for readers, I would point out that there isn’t anything wrong with placing JavaScript on a button you just need to know how you want your project to flow and use it accordingly.

Like
(1)
>
Greg Stager
's comment
2021-06-09 16:17:42
2021-06-09 16:17:42
>
Greg Stager
's comment

Thanks Greg. Yes, I confess to using a slightly clickbait-ey title, ha ha. Certainly there are use cases where either you always want to pause, or it doesn’t matter. In my world it seems I almost always have a mix of timeline-based and event-driven activities, and the binary nature of the “Continue Playing the Project” box causes problems.

Like
2021-06-07 08:15:34
2021-06-07 08:15:34

Thanks for posting this workaround. Personally I mostly use JS in a shared or advanced action in combination with other actions. But it will for sure be handy for other JS dedicated users.

May I just ask a question: why do you pause the slide On Exit? The slide on Exit event if not to be trusted, and this could confuse users. Of course you may have a specific reason for that.

Like
(1)
>
Lieve Weymeis
's comment
2021-06-09 16:10:14
2021-06-09 16:10:14
>
Lieve Weymeis
's comment

It has a reason, but it’s nothing to do with this topic so I replaced the screenshot without that in there to avoid confusion. Thanks for pointing it out.

Like
Add Comment