Milacay
Milacay

Reputation: 1497

Highlight Row when click and Un-Highlight when click somewhere else

I am new to jQuery and I try to highlight the entire row when user clicks on the cell, it works so far. However, I like to un-highlight that row was selected when user clicks some where else or another row.

Here is my code for highlight the row, but I don't know who to un-highlight it.

// Highlight Select Row
$("#mytable td").click(function() {
    $(this).css('background-color','#f00');
    $(this).parent().css("border", "yellow solid 2px"); 
});

Please help,
Thanks,

Upvotes: 0

Views: 54

Answers (1)

MDJ
MDJ

Reputation: 366

Use a class to do it, For example

    .highlight
     {
      background-color : #f00 !important;
     }

Then in your script

     $("#mytable td").click(function() {
        $("#mytable td").removeClass("highlight");
        $(this).addClass("highlight");

     });

and where ever you wanna remove highlighting, use

      $("#mytable td").removeClass("highlight");

Hope this helps.

Upvotes: 1

Related Questions