kratz
kratz

Reputation: 576

How to process the elements excluding the one selected in jquery

How can I process the elements that are not currently selected. Ex. on my html file, I have

<table>
  <tr><td> .... </tr></td>
  <tr><td> .... </tr></td>
  <tr><td> .... </tr></td>
  <tr><td> .... </tr></td>
</table>

on js file, I have a click event and choose one of the row. During this event trigger, I'd like to process the other rows.

$("table tr").click(function(){
   // process the unselected rows here such as change the background color...
});

Upvotes: 0

Views: 81

Answers (2)

Alex Gyoshev
Alex Gyoshev

Reputation: 11967

+1 on fphilipe's answer, though there is the thing that the logic of the styles should be inverted - i.e. you should style the selected row, rather than processing the other rows.

$("table tr").click(function() {
    $(this)
        .siblings().removeClass('selected').end()
        .addClass('selected');
});

And the CSS

.selected { background-color: #D6E4EE; }

Upvotes: 0

fphilipe
fphilipe

Reputation: 10054

Have a look at the siblings function.

Then it would be in the click function:

$(this).siblings();

Upvotes: 2

Related Questions