Declaring var slider too early?

Jer's Avatar

Jer

29 May, 2021 02:41 PM

When I try to set

var slider = $(".royalSlider").data('royalSlider');

anywhere in the file, the variable never sets. The console tells me slider is undefine.

If I wait for the page to load completely, then I type var slider = $(".royalSlider").data('royalSlider'); into the console manually, then the variable will be defined as the slider.

What is the right way to set the slider variable?

  1. Support Staff 1 Posted by Dmitry Semenov on 29 May, 2021 08:47 PM

    Dmitry Semenov's Avatar

    You may get instance of the slider only after it's initialized (after $('.selector').royalSlider({ }) method is executed). Which generally happens in jQuery document.ready event. https://learn.jquery.com/using-jquery-core/document-ready/

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac