So, I want to get a data-price attribute from an element. I do not want to use jQuery(simply because it looks really complex to install, for something very simple I want to do). How can I do this? I found no way to do it in javascript. I've tried just dotting after the element variable.
1 Answer
element.dataset will get you the set of an element’s data-* attributes, if supported, so:
element.dataset.price
On browsers where this isn’t supported, you can use the usual .getAttribute('data-price') and cast appropriately.
1 Comment
Kyle Needham
I never knew dataset existed thanks for the enlightenment, it's a shame IE has only just started supporting it.
document.getElementById('element').getAttribute('data-price')