0

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.

1 Answer 1

3

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
2
  • ok I was actually able to get the data using $(this).attr("data-desc") as well but I was not wanting to return them concatenated, but as an object or array. Sorry I was not clear. I edited my question.
    – skribe
    Aug 18, 2016 at 13:54
  • I have updated the code. Please check your browser console if the result is in correct format.
    – palaѕн
    Aug 18, 2016 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.