Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get attribute values as array from selection of elements using jQuery

Tags:

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.
like image 288
Guy Avatar asked Aug 11 '09 06:08

Guy


1 Answers

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.

like image 102
Christian C. Salvadó Avatar answered Sep 24 '22 17:09

Christian C. Salvadó