First, you should not have duplicate identifiers for strings. Identifiers must be unique for each element on the page.
In addition, you do not need to use each , since jQuery automatically applies the slideToggle function to each element that matches the selector. I would suggest dropping identifiers and using class names instead:
$("tr.header").click(function () { $("tr.child").slideToggle("fast"); });
If you want to make sure that only certain tables can perform this switch function, put the class in the table and update the selector:
<table class="collapsible"> <tr> <td>Cat1</td> <td>Row</td> </tr> <tr> <td>Collapsible</td> <td>Row</td> --- $(".collapsible tr:first").click(function () { $(this).nextAll().slideToggle("fast"); });
Reply to edit:
Rory's answer is correct, I'm just expanding it. If you use multiple tables, you can remove the CSS classes from <tr> in the rows and simplify the tables to:
<table class="collapsible"> <tr> <td>Cat1</td> <td>Cat1</td> </tr> <tr> <td>Collapsible</td> <td>Row</td> </tr> </table> <table class="collapsible"> <tr> <td>Cat2</td> <td>Cat2</td> </tr> <tr> <td>Collapsible</td> <td>Row</td> </tr> </table>
and jQuery before:
$(".collapsible tr:first").click(function () { $(this).nextAll().slideToggle("fast"); });
John rasch
source share