Home > Software engineering >  document on click , exclude elements
document on click , exclude elements

Time:02-18

How do i exclude same element with different class on document click

Click function as follows

$(document).on('click', 'tr.player_row', function () {

});

I want to exclude the following same type elements that have another class tr.player_row.locked_starter tr.player_row.locked_bench

Tried multiple times with variations like this

$(document).on('click', 'tr.player_row , :not("tr.player_row.locked_bench") , :not("tr.player_row.locked_starter")', function () {

});

CodePudding user response:

You can use hasClass() in your event and check class with if condition

$(document).on('click', 'tr.player_row', function () {
  if (!$(this).hasClass("locked_starter") && !$(this).hasClass("locked_bench")) {
    $(this).addClass('special');
  }
});
.special { color:red }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <tr >
        <td>player_row</td>
    </tr>
    <tr >
        <td>player_row</td>
        <td>locked_starter</td>
    </tr>
    <tr >
        <td>player_row</td>
        <td>locked_bench</td>
    </tr>
</table>

  • Related