Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 232
bart2puck Avatar asked Nov 04 '16 10:11

bart2puck


3 Answers

Maybe this is what you need. See demo below

Two options are available.

Since you're checking the text content of <th> to perform a specific action, then you can use jQuery .text() to get the content of the <th>.

Read more about jQuery .text()

You'll also need JavaScript this to refer to the currently clicked <th>.

Read more about this keyword at MDN

Option 1

This option keeps adding .unfocused class to all <th> when clicked.

$(document).ready(function() {

  $('th').click(function() {
    if ($(this).text() == "Hello") {
      $(this).addClass('unfocused');
    }
  });

});
.unfocused {
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='myTable'>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>
      <table id='otherTable'>
        <tr>
          <th>Goodbye</th>
        </tr>
      </table>
    </th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
</table>

Option 2

This option removes .unfocused class from all <th> before adding .unfocused class to the currently clicked <th>.

$(document).ready(function() {

  $('th').click(function() {
    if ($(this).text() == "Hello") {
      $('th').removeClass('unfocused');
      $(this).addClass('unfocused');
    }
  });

});
.unfocused {
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='myTable'>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
  <tr>
    <th>
      <table id='otherTable'>
        <tr>
          <th>Goodbye</th>
        </tr>
      </table>
    </th>
  </tr>
  <tr>
    <th>Hello</th>
  </tr>
</table>
like image 196
Abk Avatar answered Oct 13 '22 01:10

Abk


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')
like image 30
Krzysiek Szymczak Avatar answered Oct 13 '22 02:10

Krzysiek Szymczak


You are giving background to parent. So this solution may be useful to you.

First Way

$('#myTable th').click(function() {
    $('#myTable').addClass('unfocused');
    $('#otherTable').addClass('white');
});

Second Way

$('#myTable th').click(function() {
    $(this).parent().addClass('unfocused');
    $(this).parent().siblings().not(".check").addClass('unfocused');
});
like image 1
priya_singh Avatar answered Oct 13 '22 01:10

priya_singh