DEV Community

Discussion on: How to structure your Alpine.js Code into modules

Collapse
 
skttl profile image
Søren Kottal

Don't know if getElementsByClassName is better or faster, but you could also detect the component by doing

const hasExampleComponent = () => {
  return document.querySelectorAll("[x-data='example']").length > 0;
};
Enter fullscreen mode Exit fullscreen mode

That way, you don't have to rely on some arbitrary class name.