How to find array of properties by data attribute JavaScript – best methods? or What’s the easiest wa

16 Replies, 1561 Views

Kinda surprised no one’s mentioned `matches()` yet!

You can do:
```javascript
const admins = [...document.querySelectorAll('[data-role]')].filter(el => el.matches('[data-role="admin"]'));
```

Might be overkill for simple cases, but it’s flexible af.

Messages In This Thread



Users browsing this thread: 1 Guest(s)