zurchpet icon

table2csv with jQuery

zurchpet | PRO | 04/01/11 10:03:25 PM UTC | 0 ⭐ | 1145 👁️ | Never ⏰ | []
HTML |

1.07 KB

|

None

|

0 👍

/

0 👎

<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.2.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
    $(function(){
        $("#convert").click(function(){
            table2csv("#to_convert","#target");
        });
    });
 
    function table2csv(table, target){
        var tableRows = $(table + ' tr');
        $(tableRows).each(function(){
            $(this).children('td').each(function(){
                console.log($.trim($(this).text()));
                $(target).text($(target).text() + "\"" +$.trim($(this).text())+"\";");
            });
            $(target).text($(target).text() + "\n");
        });
    }
</script>
<table id="to_convert" border="1">
    <tr>
        <td>
            Field A1
        </td>
        </td>
            Field A2
        <td>
            Field A3
        </td>
    </tr>
    <tr>
        <td>
            Field B1
        </td>
        </td>
            Field B2
        <td>
            Field B3
        </td>
    </tr>
    <tr>
        <td>
            Field C1
        </td>
        </td>
            Field C2
        <td>
            Field C3
        </td>
    </tr>
</table><br/>
<input type="button" value="convert" id="convert" /><br/>
<textarea id="target" style="width:500; height: 200px;"></textarea>
</body>
</html>

Comments