Skip to content Skip to sidebar Skip to footer

Jquery Get All Data Attributes From A List

I have a long dynamically generated list each with the same class identifier and a data attribute similar to the code below:
  • One

Solution 1:

You can use .map to return an array:

var dataList = $(".list").map(function() {
    return $(this).data("id");
}).get();

console.log(dataList.join("|"));

Demo: http://jsfiddle.net/RPpXu/

Solution 2:

Use this:

var array = [];

$('li.list').each(function() {
  array.push($(this).data('id'));
})

var joined = array.join('|');

Solution 3:

Not an answer to your question but this is what i ended up on this page in search for and though it would help someone else :-)

var arrayOfObj = $('input').map(function() {

  return {
            name : $(this).attr('name'),
            data : $(this).data('options'),
            value : $(this).val(),
            id : $(this).attr('id')
         }

}).get();
console.log(arrayOfObj)

This returns an array of objects that mimics the input Cheers!

Post a Comment for "Jquery Get All Data Attributes From A List"