What's the best way to get data attribute JavaScript values from an element? or How do I properly use

18 Replies, 1314 Views

Hey! For grabbing data attributes in JS, I always use `getAttribute()` because it’s straightforward.

Like:
```js
const val = el.getAttribute('data-thingy');
```

But `dataset` is cool too—just depends on your style.

Btw, if your get data attribute javascript code fails, check if the element exists first. Common mistake!

Messages In This Thread



Users browsing this thread: 1 Guest(s)