Guy
Guy

Reputation: 67290

Get attribute values as array from selection of elements using jQuery

I have the following using jQuery:

var x = $('.boxes > input:checked');

From x I am trying to retrieve an array of id values and have been unable to work out how to do this.

Something like:

var y = x[id];
// y becomes an array like ['1', '2', '3'] assuming
// that x had 3 checkboxes with id's of 1, 2, 3 etc.

Upvotes: 10

Views: 6383

Answers (1)

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827416

You can use jQuery.map:

var x = $('.boxes > input:checked');

var y = $.map(x, function (element){
  return element.id;
});

The y variable, will be an array containing the element ids.

Upvotes: 19

Related Questions