Get the contents of table data in an array

I have a table that is created dynamically, I want the contents of the table in an array to mean in a variable (like var k = {1.hai 2.me 3.you ....}). I use this code, but it is not very good.

 var x = $("tr[id='1'] > td:nth-child(1)").html()
        x1 = $("tr[id='2'] > td:nth-child(1)").html();
        x2 = $("tr[id='3'] > td:nth-child(1)").html();
        x3 = $("tr[id='4'] > td:nth-child(1)").html();....
        ......
var y=[x,x1,x2,x3,.....................]
Run codeHide result

How to do this using a loop?

+4
source share
1 answer

Here's how you do it:

var td=$("table tr td:nth-child(1)"); // get first child of all the td elements

var htmlContent=[]; // initilize an empty array

for(i=0; i<td.length; i++){

    htmlContent[i]=$(td[i]).html(); // add the html content

}
+4
source

Source: https://habr.com/ru/post/1651523/


All Articles