user3007294
user3007294

Reputation: 951

Creating An Array From jQuery Selectors

If I have the table:

 <table id="mastermind_table_one">
   <tr id="one">
     <td id='first'>First</td>
     <td id='second'>Second</td>
     <td id='third'>Third</td>
     <td id='forth'>Forth</td>
   </tr>
 </table>

How would I go about creating an array that contains each td?

I was hoping to get something like:

var array = ["First", "Second", "Third", "Forth"]

Upvotes: 0

Views: 51

Answers (3)

Roko C. Buljan
Roko C. Buljan

Reputation: 206028

DEMO

var array = [];
$("#one td").text(function(i, txt) { array.push(txt); });

Upvotes: 1

Aarmora
Aarmora

Reputation: 1153

var newArray = [];
$('#one').children().each(function(){
  newArray.push($(this).html());
})

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

Like so:

var array = $("#one td").map(function() {
    return $(this).text()
}).get();

Upvotes: 5

Related Questions