Mike
Mike

Reputation: 1623

Using jQuery to Highlight Selected ASP.NET DataGrid Row

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

Upvotes: 4

Views: 4185

Answers (3)

nickf
nickf

Reputation: 546055

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');
});

Upvotes: 3

Adam Bellaire
Adam Bellaire

Reputation: 110489

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.

Upvotes: 0

Adam Bellaire
Adam Bellaire

Reputation: 110489

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

$('.toggledClass').removeClass('toggledClass');

Upvotes: 3

Related Questions