tags:

views:

2365

answers:

3

Hi,

It is easy to highlight a selected datagrid row, by for example using toggleClass in the tr's click event. But how best to later remove the highlight after a different row has been selected? Iterating over all the rows to unhighlight them could become expensive for larger datagrids. I'd be interested in the simplest solution, as well as the most performant.

Thanks,
Mike

+3  A: 

If you just want to find items that have toggledClass and turn that off using jQuery:

$('.toggledClass').removeClass('toggledClass');
Adam Bellaire
Excellent! Functional programming is beautiful.
Mike
A: 

For faster performance, you could push your selected element's ID into a var (or an array for multiples), and then use that var/iterate over that array when toggling the classes off.

Adam Bellaire
+3  A: 

This method stores the active row into a variable. The $ at the start of the variable is just my own hungarian notation for jQuery objects.

var $activeRow;

$('#myGrid tr').click(function() {
    if ($activeRow) $activeRow.removeClass('active');
    $activeRow = $(this).addClass('active');
});
nickf
Basic and high performance, gotta go with this one.
Mike