Kurkula
Kurkula

Reputation: 6762

Jquery table th class contains

I am trying to hide table columns where table header contain certain string. I also have class name for each column in table header, so as part of best practice, I am planning to update code to find th class name contains certain string. But the second scenario below fails when I am trying to get class name of th. Could some one help me in this part.

Working scenario with th contains:

var table = $('#tblProjects');
var findColumn = $(table.find('th:contains(' + columnName + ')'));

Not working scenario with class of th contains:

var table = $('#tblProjects');
var findColumn = $(table.find('th.attr('+'class'+'):contains(' + columnName + ')'));

Upvotes: 0

Views: 816

Answers (1)

Damien Black
Damien Black

Reputation: 5647

The proper way to do this, which I mention because you say you are trying to use best practices, would be to give all of the columns you want to hide a specific class like 'hideMe'. Remember, html elements can have multiple classes so this would just be an extra one on top of everything else. After that you could do:

var findColumns = $('#tblProjects th.hideMe');

Notice the selector there, '#tblProjects th.hideMe'. I am saying find all th elements with a class 'hideMe' that are inside of #tblProjects.

Understanding css selectors and how they work is critical in jQuery, have a look a this reference: css selectors

Upvotes: 1

Related Questions