Sumeru Suresh
Sumeru Suresh

Reputation: 3181

how to know click event has been fired or not in jquery?

I have the following problem to be solved.

I have few rows of data. When i click one row the color of the row has to be changed and when i click the other row the color of the previous selected row should become normal and the current row should be highlighted with the color. This is done through the following code.

Check = function()
{

    $('tr').click(function(){
        $('tr').removeClass("coloradd");
        $(this).addClass("coloradd");

    });

};

But now a complexity is added to the requirements. When i press the shift key and select multiple rows the selected rows should get highlighted. Could anyone help me out with this?

Upvotes: 0

Views: 375

Answers (2)

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

Check = function()
{

    $('tr').click(function(e){
       var code = (e.keyCode ? e.keyCode : e.which);
       if(code != SHIFT_KEY_CODE_HERE) { 
         $('tr').removeClass("coloradd");
       }

       $(this).addClass("coloradd");

    });

};

Upvotes: 0

Doug Neiner
Doug Neiner

Reputation: 66191

You can test for the shift key like this (I also included a little optimization):

$('tr').click(function(e){
    // Only run if Shift key is not held down
    if(!e.shiftKey) $('tr.coloradd').removeClass("coloradd");

    // Always select the current row
    $(this).addClass("coloradd");
});

Upvotes: 2

Related Questions