![]() |
|
What's the best way to use javascript get data attribute from an element? or How do you properly use - Printable Version +- Proxy Community (https://proxycommunity.com/forum) +-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support) +--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development) +--- Thread: What's the best way to use javascript get data attribute from an element? or How do you properly use (/thread-what-s-the-best-way-to-use-javascript-get-data-attribute-from-an-element-or-how-do-you-properly-use) |
“” - PhantomByte - 05-04-2025 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! “” - TorXplorer77 - 06-04-2025 If you’re using frameworks like React or Vue, they handle data attributes differently. In React, you’d use `dataset` but with JSX syntax like `data-thing={value}`. Vue has `v-bind:data-thing`—so framework context matters! |