Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 459
loi219 Avatar asked Nov 21 '13 09:11

loi219


People also ask

How can get TD value from TR in jQuery?

How can get TD value from TR in jQuery? var Something = $(this). closest('tr'). find('td:eq(1)').

How do I get a table cell by index using jQuery?

data. find(selector); Then to find the value of different cells in a row, we'll have to specify the cell which will be done using the eq() method in jQuery. This method returns the value with a specific index number of the selected elements.


4 Answers

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();
}
like image 111
Mohamed Rashid.P Avatar answered Sep 22 '22 09:09

Mohamed Rashid.P


Try this:

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

Example fiddle

like image 41
Rory McCrossan Avatar answered Sep 22 '22 09:09

Rory McCrossan


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/

like image 35
Chaim Leichman Avatar answered Sep 20 '22 09:09

Chaim Leichman


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();
like image 44
Rituraj ratan Avatar answered Sep 21 '22 09:09

Rituraj ratan