Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all table headers in jQuery

Tags:

jquery

How can I list all table headers in jQuery? My HTML table is below.

<table class="edit-table mobile-optimised break-words">
    <thead>
        <tr>
            <th scope="col">Location</th>
            <th scope="col">Date from/to</th>
            <th scope="col">Qty of<br>places</th>
            <th scope="col">Paid by £155.00 voucher<br>(net value)</th>
            <th scope="col">Paid by card<br>(net value)</th>
        </tr>
    </thead>
    <tbody>
        <tr class="no-records"><td colspan="5">No records to display.</td></tr>
    </tbody>
</table>

I would like to loop through each <th> and get the text value for each one.

like image 234
V4n1ll4 Avatar asked Dec 05 '22 02:12

V4n1ll4


2 Answers

You can iterate over th like following

$("table.edit-table thead tr th").each(function(){
    console.log($(this).text());
});

For reference - http://plnkr.co/edit/zphkd8pVICzQ0zZRcOfn?p=preview

like image 118
Nikhil Aggarwal Avatar answered Feb 12 '23 12:02

Nikhil Aggarwal


$( "table thead tr th" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

see documentation : https://api.jquery.com/each/

like image 33
ted Avatar answered Feb 12 '23 14:02

ted