0

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.

2
  • 1
    document.getElementById('element').getAttribute('data-price') Commented Apr 24, 2014 at 20:48
  • jQuery does not require installation. Commented Apr 24, 2014 at 20:49

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I never knew dataset existed thanks for the enlightenment, it's a shame IE has only just started supporting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.