Malarivtan
Malarivtan

Reputation: 414

Jquery HTML Table perse

I am trying to Parse HTML Table:

<table id="compare_multi" class="" data-intro="">
    <thead class='header'>
    <tr>
        <th colspan="2">
            <span class="pull-right"></span>
        </th>

            <th class="center">

            </th>

        <th></th>
    </tr>
    </thead>
    <tbody>

        <tr>
            <th class="category" colspan="" style="padding: 10px;"></th>
        </tr>
        <tr class="valuerow">

            <td style="width: 25px">122</td>
            <td></td>
            @foreach
            {
                <td class="center tdvalue">INIB</td>
                <td class="center tdvalue">4</td>
                <td class="center tdvalue">4</td>
                <td class="center tdvalue">INIB</td>
            }
            <td></td>
        </tr>

    </tbody>
</table>

Jquery:

var row = [];
    $("#compare_multi tr").each(function() 
    {
        $this = $(this);
        var tdvalue = $this.find(".tdvalue").text();
        row.push(tdvalue);

    });
    console.log(row);

I am trying to parse the Table: I am getting result like the follwoing

["INIB 4 4 INIB ", "12 4 9 1 ", "2 2 2 INIB "]

and I want it to be like this:

Array 1:
 0: INIB
 1: 4
 2: 4
 3: INIB

Array 2:
.
.
.
.
.

What am I doing wrong? please help

Upvotes: 0

Views: 60

Answers (1)

Ronen Cypis
Ronen Cypis

Reputation: 21470

The problem with your code is that $this.find(".tdvalue").text() returns a string, which is concatenation of all of the ".tdvalue" texts, and not an array like you expected. You should do a separate .each() on this array, in order to get the separated td's values into an array (for each tr in your table.

Considering your HTML, you probably meant this:

var rows = [];  // the result array
    $("#compare_multi tr").each(function() {
        $tr = $(this);
        var row = [];    // array for each tr
        $tr.find(".tdvalue").each(function(){
            row.push($(this).text());   //separate value for each tdvalue
        });
        rows.push(row);

    });
    console.log(rows);

Upvotes: 1

Related Questions