beebek
beebek

Reputation: 1949

grab column elements from table header

I have a table which has a search field. After searching data, the listed data are to be exported into excel which requires some extra information to be added which is not shown in the table, needs to be pulled from database. So, I need to access all the listed data's id (after seraching). Also the tag has no relevant 'id' or 'class' so that it can be grabbed, since it were rendered using datatables. How can this be achieved using jquery

code snippet

<table id='mytable' border='1'>
    <tr>
        <th id='ID'>ID</th>
        <th id='Email'>Email</th>
    </tr>
    <tr>
        <td>1</td>
        <td>[email protected]</td>
    </tr>
    <tr>
        <td>2</td>
        <td>[email protected]</td>
    </tr>
    <tr>
        <td>3</td>
        <td>[email protected]</td>
    </tr>
</table>

<button id='gen'>Generate excel file</button>

jsfiddle: http://jsfiddle.net/xLA3g/2/

Upvotes: 0

Views: 63

Answers (2)

caramba
caramba

Reputation: 22480

have a look at this, may it helps http://jsfiddle.net/xLA3g/3/

$('#mytable tr th').each(function(i){
    alert($(this).text());
});

ANSWER UPDATE, show td column number http://jsfiddle.net/xLA3g/4/

$('#mytable tr td:first-child').each(function(i){
    alert($(this).text());
});

let me know if I can help assist any further

Upvotes: 1

Vit Kos
Vit Kos

Reputation: 5755

Modify a little @carambas answer

var ids=[];
$('#mytable tr th').each(function(i){
    alert($(this).attr('id'));
    ids.push($(this).attr('id'));

});
console.log("ID",ids);

Upvotes: 1

Related Questions