0

How do i get the value of an html5 attribute using jQuery ?

<button data-direction="next" data-next="2"></button>

I want to be able to get the newest set value of data-next.

When i change the value dynamically using javascript and then try to get it using $('[data-direction="next"][data-next]').data('next'), it always returns the data as set when loading the page not after being updated - in this case 2.

2 Answers 2

2

$('[data-direction="next"][data-next]').attr('data-next') should work..

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

2 Comments

+1 data-next is not a true html5 data notation, but simply an html attribute.
You access them like any other attribute – using attr(). You also can't access most HTML-values by their name.
0

I concur with @Pierre. Use the attr() method to retrieve the desired value.

A point to note, your selector could turn out to be slower. Check this article out.

Comments

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.