jquery - Change class in all TDs that are children to a given TH? -
simple question (i think) - want add checkbox few table headers (th) in given table, , when checked should add class tds in column, , vice versa - when unchecked should remove class tds in specific column.
is doable jquery? can't figure out...
it possible. here 1 solution.
$("th input[type=checkbox]").on("click", function () { var index = $(this).parent().index() + 1; $("td:nth-child(" + index + ")").toggleclass("highlight"); });
table, tr, th, td { border: 1px solid black; border-collapse: collapse; } .highlight { background-color: red; color: white; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>column 1 <input type="checkbox" /> </th> <th>column 2 <input type="checkbox" /> </th> </tr> </thead> <tbody> <tr> <td>cell 1</td> <td>cell 2</td> </tr> <tr> <td>cell 3</td> <td>cell 4</td> </tr> <tr> <td>cell 5</td> <td>cell 6</td> </tr> </tbody> </table>
Comments
Post a Comment