Search code examples
jquerycsscomposition

Add color on column with jquery


How can I add css class on odd columns in a table with jquery?

My code is:

$("table columns:odd").addClass("colors");

Solution

  • You have to target td tags inside your table, and you have to use nth-child with odd:

    $("table td:nth-child(odd)").addClass("colors");
    

    You can also do it more simple:

    $("table td:odd").addClass("colors");