Perhaps this is what you need. See Demo below.
Two options are available.
Since you are checking the text content of <th> for a specific action, you can use jQuery .text() to get the content of <th> .
More on jQuery.text ()
You will also need JavaScript this to refer to the current <th> click.
Read more about this keyword in MDN
Option 1
This parameter continues to add the .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 parameter removes the .unfocused class from all <th> before adding the .unfocused class to the current <th> click.
$(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>
source share