bart2puck bart2puck - 1 month ago 8
HTML Question

Jquery selector all cells in table except

I am trying to find the correct selector to be able to change the background to blue if any Hello cell is clicked, but not if Goodbye is clicked. Adding a class to the Hello cells is a possible option, but not preferred.



$(function() {
$('#myTable td').click(function() {
$('#myTable').addClass('unfocused');
});
});

.unfocused {
background-color: blue;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='myTable'>

<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>

<tr>
<td>
<table id='otherTable'>
<tr>
<td>Something</td>
</tr>
</table>
</td>
</tr>





I have tried:

$('#myTable td').not('#otherTable td').click(....
$('#myTable td').not('#otherTable').click(....


and those dont work.

attached a fiddle.

Fiddle

Answer

Ok, got it, I think you are loooking for something like this

https://jsfiddle.net/smqdx20x/

$('#myTable th').not($('th > table#otherTable').parent()).not('table#otherTable th')