themerlinproject themerlinproject - 1 year ago 60
jQuery Question

only select one row at a time using jquery

So I am using

to highlight rows on mouseover and add a highlight class on click:

//Mouseover any row by adding class=mouseRow
$(".mouseRow tr").mouseover(function() {
$(".mouseRow tr").mouseout(function() {
$('.mouseRow tr').click(function(event) {

The above code will allow a user to 'highlight' (i.e add class
) to as many rows as they want. What is the best way, using jQuery, to limit the number of rows they can select to just one (so that if they click one row, then click another it will remove the '
' class from the previously selected row)?


Answer Source

You could remove the selectRow class from all of the tr elements except the clicked one whenever you click on one, and then toggle it on the clicked one:

$('.mouseRow tr').click(function(event) {
    $('.mouseRow tr').not(this).removeClass('selectRow');

Here's a working example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download