Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

Loop Table Get All Text Not Working Well?

I have a table like so:

  <tbody><tr><td id="589" class="btn btn-primary"><label>tarts</label><tr><td>class: Article</td></tr><tr><td>case: Accusative</td></tr></td></tr></tbody>

And need to get the Text from each element. I have this code which returns too many results like so in FireFox console:

0: Array[3]
    0: "tartsclass: Articlecase: Accusative"  //yes it's run together like this
    1; "class: Article"
    2. "case: Accusative"
    length: 3
1: Array[1]
    0: "class: Article"
    length: 1
Array[1]
    0: "class: Accusative"
    length: 1

My goal is to extract the text and build a Json string somehow...

[my code]
       function getTableData(table) {
            var data = [];
            table.find('tr').each(function (rowIndex, r) {
                var cols = [];
                $(this).find('th,td').each(function (colIndex, c) {
                    cols.push(c.textContent);
                });
                data.push(cols);
            });
            return data;
        }

[Usage]
     getTableData($('#table1 table:last'));
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WorknHardr
WorknHardr

ASKER

Yea, the html is dynamic and rough, working on it. I'll try your code snippet tonight. Thx
The html has nested TR's inside TR's which is invalid html.
thx, also got the table appends working great...