1

I am trying to remove background color on click for tables first and last rows. I am trying in the below way. Any optimized way to handle this so that I can call only one time?

//Except First row
 $('#tblNames').on('click', 'tr:not(:first-child)', function () {
        var state = $(this).hasClass('highlighted');
        $('.highlighted').removeClass('highlighted');
        if (!state) { $(this).addClass('highlighted'); }
    });

//Except Last row
 $('#tblNames').on('click', 'tr:not(:last-child)', function () {
        var state = $(this).hasClass('highlighted');
        $('.highlighted').removeClass('highlighted');
        if (!state) { $(this).addClass('highlighted'); }
    });
Kurkula
  • 6,386
  • 27
  • 127
  • 202

1 Answers1

1
 $('#tblNames').on('click', 'tr:not(:first-child), tr:not(:last-child)', function () {
        var state = $(this).hasClass('highlighted');
        $('.highlighted').removeClass('highlighted');
        if (!state) { $(this).addClass('highlighted'); }
    });

Source for the answer: jQuery on() method on multiple selectors

Community
  • 1
  • 1
Gyhth
  • 1,155
  • 9
  • 21