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

22 Replies, 1542 Views

Using `.dataset` is my go-to for javascript get data attribute! It’s clean and easy to read.

Like: `element.dataset.someValue` instead of `getAttribute('data-some-value')`.

Only downside? It doesn’t work in super old browsers, but who cares about IE these days? 😅

For performance, `.dataset` is slightly slower than `getAttribute()`, but unless you’re doing thousands of ops, it won’t matter.

Messages In This Thread



Users browsing this thread: 1 Guest(s)