skribe
skribe

Reputation: 3615

How to get value and data attribute form input using jQuery .map function

I have a series of checkbox inputs with the following format.

<input type="checkbox" name="xyz" value="124" data-info="some info">

I am using jquery to select the inputs that are checked and trying to get the value and data-info using the .map() function and use it in an ajax request. I can get the value like this.

checked_options = $(button).parent().find('input:checkbox:checked').map(function () {
      return this.value;
    }).get();

But I can't figure out how to get the value and the data-info and return them as an array or object.

Upvotes: 0

Views: 2842

Answers (1)

palaѕн
palaѕн

Reputation: 73906

You can use .data() method like:

checked_options = $(button).parent().find('input:checkbox:checked').map(function () {
  return this.value + ',' + $(this).data('info');
}).get();

Edit:

You can create an array of objects containing value and data-info like

checked_options = $(button).parent().find('input:checkbox:checked').map(function() {
  return {
    v: this.value,
    d: $(this).data('info')
  };
}).get();

console.log(checked_options); // Array of objects as result here

Upvotes: 3

Related Questions