Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve data attribute values for jQuery collection's elements

This works for the first match:

 var attributeValue = $({selector}).data("myAttribute");

But if I want to get values for all elements selector matches, I do the following:

var attributes = [];
$.each($({selector})), function(k,v) { attributes.push($(v).data("myAttribute")); });

This feels stupid. Is there simpler way to get the data for all the elements?

like image 534
Fdr Avatar asked May 15 '13 05:05

Fdr


People also ask

How get data attribute value in jQuery?

To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).

How do you get the value of a data attribute?

To get a data attribute through the dataset object, get the property by the part of the attribute name after data- (note that dashes are converted to camelCase). Each property is a string and can be read and written. In the above case setting article.dataset.columns = 5 would change that attribute to "5" .

How do you get all elements with a data attribute?

To get all DOM elements by a data attribute, use the querySelectorAll method, e.g. document. querySelectorAll('[data-id]') . The querySelectorAll method returns a NodeList containing the elements that match the specified selector.

How do you get the value of data attribute in JS?

We can either use the dataset property to get access to the data attributes or use the . getAttribute() method to select them by specifically typing their names.


2 Answers

I don't think there is a built-in way to make an array like you want. But you can certainly simplify the code a tiny bit with $().map():

var attributes = $({selector}).map( function( i, element ) {
    return $(element).data( 'myAttribute' );
});

Or if this is something that might be used in more than one place, you could make it a plugin:

$.fn.dataArray = function( name ) {
    return this.map( function( i, element ) {
        return $(element).data( name );
    });
};

and call it with this very simple code:

var attributes = $({selector}).dataArray( 'myAttribute' );
like image 133
Michael Geary Avatar answered Oct 18 '22 22:10

Michael Geary


Inspired by Michael Geary's answer above, I have come up with a similar solution.

This is for anyone that wants to retrieve data attributes and create markup with those attributes.

Background: I wanted to retrieve data attributes from an element and create html markup on the fly for as many elements that are in the parent, using those data attributes in the same dynamic markup. This is what I came up with after reading this post. This was for an event timeline.

$({selector}).map(function() {
        var foo = $(this).data('foo');
        var bar = $(this).data('bar');
        $({anotherSelector}).append('<li><a href="#0" data-foo="'+foo+'">'+bar+'</a></li>');
});
like image 33
Studocwho Avatar answered Oct 19 '22 00:10

Studocwho