question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Instantiate multiple instances with the same selector

See original GitHub issue

I’m trying to run multiple instances of Tabby on a page and can get this to work if I give each instance a unique selector and instantiate them independently:

<script>
var tabs = new Tabby('[data-tabs-1]');
var tabs = new Tabby('[data-tabs-2]');
</script>

Is there instead a way to instantiate all instances on the page which share a common selector?

Many thanks in advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
danielpostcommented, Apr 24, 2019

This doesn’t seem to be possible at the moment, since Tabby runs querySelector on the selector, which by default only returns the first instance of the elements matching the selector.

You could probably solve this by running your own querySelectorAll to find all instances of your tabs, then initiate all of them separately. Try something like this (untested):

const tabSelectors = document.querySelectorAll('[data-tabs]');

for (const [i, tabs] of [...tabSelectors].entries()) {
    tabs.setAttribute(`data-tabs-${i}`, '');
    new Tabby(`[data-tabs-${i}]`);
}
1reaction
danielpostcommented, Apr 6, 2020

@arnojong I think this should work (untested):

const tabsSelectors = document.querySelectorAll('[data-tabs]');

for (const [i, tabsContainer] of [...tabsSelectors].entries()) {
    tabsContainer.setAttribute(`data-tabs-${i}`, '');
    const tabs = new Tabby(`[data-tabs-${i}]`);

    tabs.toggle('#harry');
}

Please note that all code needs to be within the for..of loop

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript prototyping reuse on multiple instances of the same ...
Finally, I want to invoke this prototypal 'plugin' each time per selector using the same selector, for example: var hello = new Hello('....
Read more >
Matching Multiple Selectors On The Same ... - Ben Nadel
Multiple selector matches on a single element leads to only a single Directive instance. As you can see, even through we matched both...
Read more >
Use multiple TinyMCE instances in a single page | Docs
Multiple editor instances sharing the same configuration. The following example breaks the page into two separate editable areas. Each area shares a single ......
Read more >
Matching Multiple Selectors On The Same Element ... - Vimeo
Ben Nadel demonstrates that matching multiple attribute selectors on a single HTML element only produces one Directive instance for that ...
Read more >
How to avoid Angular injectable instances duplication
Basically I want to use the same settings for a whole application: ... But sometimes Angular can create more than one instance of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found