Global plugin variable

Hello Experts!

Need your help with something that I have been trying to work out.

I have a plugin with multiple elements which are kind of interconnected. So, I need to have a way to count the number of elements added.

So far, I was able to setup a variable using the window.variable approach which works fine when different elements are added to the page, however, when the same element is added more than once, it does not work.

To give context, assume in my plugin, I have defined element1, element2 and element3.
If the user adds 1 instance of element1 and two instances of element2, then the value of the variable should be 3 as in the number of instances/elements.

Is there a way I could accomplish this?

Thanks a lot for your expert advice!

Just a random guess.

Give each element a unique id, look for element via the id .

Ankur@Nocodetalks
Checkout Bubble Plugin Development Course?

Thanks for the feedback Ankur, I tried a few more things and this seems to be a bug of some sort:

So, in my plugin editor, let’s say I create 2 elements: element1 and element2.

In each of the elements, under the element code in each of the headers, I initialize the same window.variable = [] with a different value: in element1, window.variable[0]+=1 and in element2, window.variable[1]+=1.

Now, when I run the plugin, assuming I only have element1 and element2 added once each, I correctly get: window.variable = [1,1].
However, if I were to add the same element1 (or element2), on the page, it seems to not run the html code again:
So, I get window.variable = [1,1], while I should get [2,1] as in, it should rerun the element1 initialization code, however, it seems to only run it once irrespective of the number of element instances.

so, it would do something like:

  • Header code of first instance of element1
  • Header code of element2 (this updates correctly the same variable)
    - Does NOT run header code of second instances of element1
    then the remaining flows:
  • Initialize code of element1, update code of element1
  • Initialize code of element2, update code of element2
  • Initialize code of the second element1, update code of second element1

Did anyone face a similar issue when trying to add the same element multiple times to a page and when you need to concatenate or merge the values of both elements in your code?

Note: Although the initialize and update codes do run for each instance of any element, I am unable to use those as they get executed one after the other, what I am aiming for is to initialize a variable that “counts” the number of elements present in the page and then use it as a condition in the “update” of each element.

Thanks a lot

@keith @ankur1 would really appreciate your opinion on this, did you face a similar issue or could recommend any possible workaround?