Add jQuery class for second table columns

<table> <tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr> <tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr> <tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr> <tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr> <tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr> </table> table td { background-color: green; padding: 5px; border: 1px solid blue; } .red { background-color: red; } 

LIVE: http://jsfiddle.net/zCduV/1/

How can I add a .red class with jQuery for the second column in this table (in this example, where is where in td there is ccc)?

+6
source share
5 answers

Could it be?

 // selects both table header and table data cells from the second column of the table $('table th:nth-child(2), table td:nth-child(2)').addClass('red'); 

http://jsfiddle.net/tdTkQ/

+12
source

Using the nth-child jQuery selector:

 $('td:nth-child(2)').addClass('red'); 

Link: http://api.jquery.com/nth-child-selector/

+11
source

Take a look at jQuery nth-child-selector . This is what you are looking for.

  $('td:nth-child(2)').addClass('red'); 
+2
source
 $('td:nth-child(2)').addClass('red'); 
+2
source

An example of this solution.

+1
source

Source: https://habr.com/ru/post/906938/


All Articles