loi219
loi219

Reputation: 248

How to get a cells value in html table with JQuery

I seen a lot of post talk about my problem but any of one work for me.

I've this html table, I would to get the values under the cell (th) "Index". How can I use jQuery for making this:

<table id="htmlTable">
    <caption>Informations des hotspots</caption>
    <thead>
        <tr>
            <th>Index</th>
            <th>Nom du hotspot</th>
            <th>Image du hotspot</th>
        </tr>
    </thead>
    <tbody>
        <tr id="0">
            <td>0</td>
            <td>Hotspot Fribourg Centre</td>
            <td>../images/logos_hotspot/logo_wifi_centre.png</td>
            <td>
                <input type="button" value="supprimer" />
            </td>
        </tr>
        <tr id="1">
            <td>1</td>
            <td>Hotspot Avry Centre</td>
            <td>../images/logos_hotspot/logo_wifi_avry.png</td>
            <td>
                <input type="button" value="supprimer" />
            </td>
        </tr>
    </tbody>
</table>

Upvotes: 8

Views: 84210

Answers (6)

CodeToLife
CodeToLife

Reputation: 4141

 let s_=jqueryDTtable.column(m).nodes()[n].textContent

where m is column number in the row started from 0 and n is your node's order number from 0 to column nodes' count-1.

Upvotes: 1

Son Cao
Son Cao

Reputation: 93

Try the code below.

$(document).on("click", "[id*=tableId] tr", function () {

   var a = $(this).find("td").eq(3).children().html(); 

   alert(a);
});

Upvotes: 1

Rituraj ratan
Rituraj ratan

Reputation: 10378

by this th relative td value come

 var tharr=[];
    $("#htmlTable").find("tbody tr").each(function(){
    tharr.push($(this).find("td:eq(0)").text());

    });

alert(tharr.join(",")); //by this you get 0,1

and if you want only th value do this

$('#htmlTable tr th:first').text();

Upvotes: 2

Mohamed Rashid.P
Mohamed Rashid.P

Reputation: 183

I Think This Will Help You

var MyRows = $('table#htmlTable').find('tbody').find('tr');
for (var i = 0; i < MyRows.length; i++) {
var MyIndexValue = $(MyRows[i]).find('td:eq(0)').html();
}

Upvotes: 13

Chaim Leichman
Chaim Leichman

Reputation: 118

To get the content of the <th> tag itself:

$('#htmlTable th:first').html()

To traverse the subsequent <td> tags and get their values:

$('#htmlTable tr:gt(0)').each(function(){
    console.log($('td:first', $(this)).html());
});

Or fiddle with it yourself: http://jsfiddle.net/chaiml/p2uNv/4/

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337560

Try this:

var text = $('#htmlTable tr th:first').text(); // = "Index"

Example fiddle

Upvotes: 2

Related Questions