Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the table cells value row by row with jquery

Tags:

jquery

I have a table like this

<table class="wp-list-table widefat fixed posts">
        <tbody id="c_b">
    <tr>
        <td><b>Title</b></td>
        <td><b>Upcharge</b></td>
        <td><b>Profit Percentage</b></td>
        <td><b>Short Description</b></td>
    </tr>
    <tr>
        <td colspan="4"><input type="checkbox" id="selectall"> Bulk <u>Check/Uncheck</u></td>
    </tr>
    <tr>
        <td id="title_1"><input type="checkbox" id="product_1" value="123123_1" class="case"> 123123<br></td>
        <td id="upcharge_1">24</td>
        <td id="percentage_1">15</td>
        <td id="sdescription_1">This is a short</td>
    </tr>
    <tr>
        <td id="title_2"><input type="checkbox" id="product_2" value="A33232_2" class="case"> A33232<br></td>
        <td id="upcharge_2">24</td>
        <td id="percentage_2">15</td>
        <td id="sdescription_2">This is a short</td>
    </tr>
    <tr>
        <td id="title_22"><input type="checkbox" id="product_3" value="BEY-049_22" class="case"> Plane Shirt<br></td>
        <td id="upcharge_22">24</td>
        <td id="percentage_22">15</td>
        <td id="sdescription_22">SD for Plane shirt</td>
    </tr>
    <tr>
        <td id="title_23"><input type="checkbox" id="product_4" value="IRCTC_23" class="case"> Rail Neer<br></td>
        <td id="upcharge_23">24</td>
        <td id="percentage_23">15</td>
        <td id="sdescription_23">Rail neer short description</td>
    </tr>
    <input type="hidden" value="47474" id="licence_no" name="licence_no"><input type="hidden" value="47474" id="licence_no" name="licence_no">
    <input type="hidden" value="47474" id="licence_no" name="licence_no"><input type="hidden" value="47474" id="licence_no" name="licence_no">      
    </tbody>
    </table>

I want to get the cells value row by row as an array, for this I write the code like below

$("tbody>tr").each(function(){
        var rowvalue = [];
        $("tbody>tr>td").each(function(){
            //alert($(this).text());
            rowvalue.push($(this).text());
        });
        alert(rowvalue);

    });

Here I am geting the all values at a time and it is alerting n times (n= number of row), but I want n number of array with that rows value. How can I get those value.

like image 695
Bidyut Avatar asked Sep 13 '12 15:09

Bidyut


People also ask

How can get TD table row value in jQuery?

$('#mytable tr'). each(function() { var customerId = $(this). find("td:first"). html(); });

How can get row number in HTML table using jQuery?

You can use the Core/index function in a given context, for example you can check the index of the TD in it's parent TR to get the column number, and you can check the TR index on the Table, to get the row number: $('td'). click(function(){ var col = $(this). parent().

How do I get the values of columns for a selected row using jQuery?

btnSelect',function(){ // get the current row var currentRow=$(this). closest("tr"); var col1=currentRow. find("td:eq(0)"). text(); // get current row 1st TD value var col2=currentRow.


2 Answers

You can use this,

var rowvalues = [];
            $("tbody > tr").each(function () {
                var rowvalue = [];
                $(this).children().each(function () {
                    //alert($(this).text());
                    rowvalue.push($(this).text());
                });
                rowvalues.push(rowvalue);
            });

here rowvalues array will contain row wise values

like image 187
SaminatorM Avatar answered Nov 15 '22 09:11

SaminatorM


Demo: http://jsfiddle.net/iambriansreed/2Pgey/

var table_data = [];

$('tr').each(function(){

    var row_data = [];    

    $('td', this).each(function(){

        row_data.push($(this).text());   

    });    

    table_data.push(row_data);

});

console.log(table_data);
like image 39
iambriansreed Avatar answered Nov 15 '22 08:11

iambriansreed