tags:

views:

162

answers:

1

Hi, I'm trying to loop through a table to create a csv string. I do some sorting through jquery on the table so I want to be able to get the resulting sorted data. Problem is I want to identify each new row, so add a \n or something after the last td of every row. The code below just isnt right. Any pointers appreciated.

function GetCsv() {
        var strCSV = '';
        $('#MyTable tr').each(function() {
            var lis = $(this).siblings;
            for (var i = 0; i < lis.length; i++) {
                strCSV += $(this).find("td").eq(i).html();
            }
            strCSV += 'New line';
        });
    }
+2  A: 

try:

function GetCsv() {
        var strCSV = '';
        $('#MyTable tr').each(function() {
            $(this).children().each(function(){
                strCSV += $(this).html();
            })
            strCSV += 'New line';
        });
    }
Reigel
+1 This should work. I would declare intermediate variable `var tds = $(this).children();` so that the solution would be as similar to the original post as possible.
Anton