1

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

2
  • 7
    This question is poorly worded. Can you explain what you mean, what you have, what you want? Commented Aug 22, 2011 at 8:49
  • 2
    Maybe also tell us how you want to retrieve the values. I'm guessing you'll want to store the values in a 2-dimensional array (rows and columns) so that you could use a function such as getTableDatafrom(row, column) to get the value of a specific <td> in a specific <tr>. Commented Aug 22, 2011 at 9:09

4 Answers 4

5

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/

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1
$('#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>

Comments

1
// 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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.