Hearaman
Hearaman

Reputation: 8726

How to store table cell values in a jquery array?

How do I store table cell values in a jquery array?

Upvotes: 1

Views: 12172

Answers (4)

IfThenElse
IfThenElse

Reputation: 509

// USE: var myTableArray = table2array( $('#idTable') );
function table2array( jTable ) {
    tableArr = new Array();
    var ix = 0;
    $(jTable).find('tr').each(function(){
        tableArr[ix] = new Array();
        $(this).find('th, td').each(function(){
            tableArr[ix].push($(this).text().trim());
        });     
        ix++;
    });     
    return tableArr;
};

Upvotes: 1

Harry
Harry

Reputation: 11

$('#mytable').map(function() {
            var data_array = $(this);
});

Your table:

<table id="mytable">
<tr>
    <td>improve your</td>
</tr>
<tr>
    <td>accept rate plz!</td>
</tr>

Upvotes: 1

Rafay
Rafay

Reputation: 31033

iterate over each table cell and push its value in the array say for example you have table structure like

<table>
    <tr>
        <td>improve your</td>
    </tr>
    <tr>
        <td>accept rate plz!</td>
    </tr>

</table>

you can do

var arr=[];

$("td").each(function(){

arr.push($(this).text());
});

$.each(arr,function(index,value){

alert(arr[index]);
});

here is the fiddle http://jsfiddle.net/qNgST/1/

Upvotes: 5

rickyduck
rickyduck

Reputation: 4084

tableArr = new Array();
$('table td').each(function(){
    tableArr.push($(this).text());
});

Upvotes: 2

Related Questions