Javier S
Javier S

Reputation: 115

jquery - select class elements and add to array

I want to select all the elements with the class "play" and add them into array "esArreglo".

html file:

<td id="b1" class></td>
<td id="b2" class></td>
<td id="b3" class="play"></td>
<td id="b4" class="play"></td>

code:

$('td.play').each(function() {
    notas.push(this.id)};

var esArreglo = notas.join(',');

How can I fix this? Thanks.

Upvotes: 0

Views: 219

Answers (2)

xdazz
xdazz

Reputation: 160883

You could use jQuery.map

var esArreglo = $('td.play').map(function() {
  return this.id;
}).get().join(',');

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

May coz be you are missing the ); in .each

$('td.play').each(function() {
    notas.push(this.id)
});

var esArreglo = notas.join(',');

Upvotes: 0

Related Questions