What's the best way to use javascript get data attribute from an element? or How do you properly use

22 Replies, 1461 Views

For edge cases, like dynamic elements, combine `getAttribute()` with event delegation.

Example:

```
document.addEventListener('click', (e) => {
const value = e.target.getAttribute('data-action');
if (value) { /* do stuff */ }
});
```

Works like a charm!

Messages In This Thread



Users browsing this thread: 1 Guest(s)