stack stack - 1 month ago 8
Javascript Question

How to make a array of html content?

Here is the HTML:

<table>
<tbody>
<tr>
<td>1</td>
<td>one</td>
</tr>
<tr>
<td>2</td>
<td>two</td>
</tr>
</tbody>
</table>


Now I need to make an array of second column of that table like this:

var arr = ['one', 'two'];


How can I do that?

I can select the table like this
$('table')
and get its content like this:
$('table td+td')
, but I don't know how can I make an array of them.

Answer

You can use :nth-child() to select second td and map() to return array DEMO

var arr = $('table td:nth-child(2)').map(function() {
  return $(this).text();
}).get();