user1069609 user1069609 - 1 month ago 7
jQuery Question

JQuery expression returning the first table row as an array of strings?

I'm new to JQuery. I need a JQuery expression returning the first table row as an array of strings?

<table id='table1'>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<table>


In this case it should give
['A','B','C']
.

Answer

You can use the .map() jQuery function to create a new array from a jQuery object, like so:

var newArray = $('#table1 tr:first td').map(function() {
    return $(this).html();
});

Working DEMO

Comments