4

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.

0

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Should you not mark it as duplicate instead?
1

Try this:

Use $("table td:nth-child(2)").each() to loop through all 2nd td and get its text using .text()

var arr = new Array();
$("table td:nth-child(2)").each(function(i){
  arr[i] = $(this).text();
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.