tags:

views:

33

answers:

2

Hey all,

I am using a jQuery's toggleClass() method to handle table row highlighting and what I would like to do is create a function that will hide all table rows that do not have the "highlight" class applied.

The table itself has an ID (tblTest) and each row has an ID as well. However in this case I don't really care about ID's so much as whether or not the "highlight" class is applied to the row. What is the best approach for essentially walking through each table row, checking to see if the "highlight: class is applied and if it is NOT then apply the "hidden" class.

Thanks,

+3  A: 

To use jQuery to hide them:

$("#tblTest tr:not(.highlight)").hide();

To apply your hidden class:

$("#tblTest tr:not(.highlight)").addClass("hidden");

You should generally favour the jQuery effects for hiding things however.

cletus
lol that was substantially more simple that I thought, thanks cletus :)
Nicholas Kreidberg
You're welcome.
cletus
Quick follow-up question: if I *never* want to hide #tblTest #thead (thead = the id set to the header row) what would I throw in with that?
Nicholas Kreidberg
$("#tblTest tbody tr:not(.highlight)").hide();
cletus
I am not actually using tbody/thead/tfoot tags, I just have my first table row (id: "thead") consisting of <th> tags (headers) and I don't even want those to be hidden. Is there a way to add an OR condition to: $("#tblTest tr:not(.highlight)").toggleClass("hidden"); so that if tr:not(.highlight) OR tr:not(#thead) ?
Nicholas Kreidberg
You should be using thead. That's what it's for.
cletus
Otherwise you could try $("#tblTest > :not(#thead) tr:not(.highlight)").hide();
cletus
Done and done, thanks again cletus.
Nicholas Kreidberg
This also works: $("#tblTest tr[id!='thead'][class!='highlight']").addClass("hidden");
fudgey
Opps, but only if "highlight" is the only class used... I couldn't get Cletus' last one to work, but I tested this and it does: $("#tblTest tr[id!='thead']:not(.highlight)").addClass("hidden");
fudgey
I went ahead and added the thead/tfoot/tbody tags to the table and everything works w/ Cletus' suggestion! woot
Nicholas Kreidberg
A: 
$('#tblTest tr:not(:has(.highlight))').slideUp('fast');
cballou