clobber
clobber

Reputation: 11

parse data-* with jquery

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

Upvotes: 1

Views: 225

Answers (3)

Maksim Vi.
Maksim Vi.

Reputation: 9225

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.

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163238

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...
    });
});

Upvotes: 3

kobe
kobe

Reputation: 15835

Are you talking about data attributes ,

this jquery metata plugin is really good

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

Upvotes: 0

Related Questions