DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in

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.