views:

472

answers:

3

I have JS function which parses through a table:

// id contains a message id, activeRow is "this" from onClick on tr
function doSomething ( id, activeRow ) {
    // AJAX calling using id as parameter
    $("#searchResultTable > tbody > tr").each(function(index) {
        $(this).removeClass("bold");
    });
}

This works perfectly fine (thanks to Ariel @ other post), but I was thinking that there should be another possibility, like:

var table = $(activeRow).parent();
$("tbody > tr", table).each(function(index) {
    // .. do something
});
// The above clearly doesn't work, but should document what I'm looking for.

This would allow the usage of the same table ID while the function would work on each of them separately.

Many, many thanks!

+1  A: 

Close, you would want to use a class on the table

$("table.className tbody tr").each(function() {
    // .. do something
});
SeanJA
+1  A: 

jQuery's parents() method makes getting the parent table a snap:

$(activeRow).parents('table')[0];
brianreavis
+2  A: 

What about:

$(activeRow).siblings().andSelf().removeClass("bold")

That'll take the <tr> in activeRow, grab its sibling <tr>s too, and remove the "bold" class from all of them.

Crescent Fresh