Search code examples
jqueryvariablesinstance

How I can use a variable as part of a instance name


Inside jQuery, I need to call a javascript instance. This is an instance of splidejs.com inside of bricksbuilder.io. The aim is to switch to a certain slide, for example:

bricksData.splideInstances.alfhxo.go(0);
bricksData.splideInstances.dxtzad.go(0);

As you can see, dynamically I need to change the third part of the instance name (which references the respective Splide attribute).

I tried to include the variable in different ways (at the end only guessing). But -of course- this is not working:

var_gamma = dxtzad;

'bricksData.splideInstances.' + var_gamma + '.go(1)';
bricksData.splideInstances.var_gamma.go(0);
bricksData.splideInstances.[var_gamma].go(0);
bricksData.splideInstances.$(var_gamma).go(0);

Any idea how I can this achieve? If more context is needed, please let me know.


Solution

  • Just use the [ notation ]:

    var gamma = "dxtzad";
    bricksData.splideInstances[gamma].go(0);