Liza
Liza

Reputation: 197

How to set a css class to table header cell

I have a table with thead cells. I want to set a css Class to 'th' only where the value of this 'th' is "ID".

I tried this :

var T = [];
$('.tbl > thead > tr > th').each(function(){
    T.push($(this).text())
 })
for (var i = 0; i < T.length; i++) {
    if (T[i] == "ID" ) {$('.tbl thead tr th').addClass('Myclass')};
}

But this Code set the class 'Myclass' to all headers of my table.

<thead> 
    <tr>  
        <th class="Myclass">ID</th> 
        <th class="Myclass">NOm</th>
    </tr>
</thead>

here is a fiddle --> http://jsfiddle.net/1ck4joum/1/

Thanks.

Upvotes: 0

Views: 2315

Answers (1)

Ashot Khanamiryan
Ashot Khanamiryan

Reputation: 1134

use this

$('.tbl thead tr th:contains("ID")').addClass('Myclass')};

Upvotes: 4

Related Questions