1

How can I parse data-* attributes with JQuery? Is something like $('a').attr('data-*').each(function(){...}); possible? Is there a simple method?

3 Answers 3

3

I think you're trying to do something like this:

$('a').filter(function() {
    //if this function returns false, it will not be included in the set.
    return $(this).data().length > 0;
}).each(function() {
    //iterate over every matched DOM element
    //and iterate over their data attribute:
    $.each($(this).data(), function(key, value) {
        //do something with key and value here...
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

There is a difference between HTML5 data-* attributes and jQuery data() storage; I think the OP is asking about the former and you are answering for the latter.
0

Are you talking about data attributes ,

this jquery metata plugin is really good

http://plugins.jquery.com/project/metadata

Comments

0

Try this http://jsfiddle.net/6tv5y/1

<div id="mydiv" data-name="negative" data-from="stackoverflow"></div>

var mydata = $('#mydiv').data();
$.each(mydata ,function(i) {                               
        alert(mydata[i]);
});

UPD: Note that it is supported only since jQuery 1.4.3.

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.