Reputation: 738
I'm using jQuery to expand a row in a table when the row is clicked. How do I modify the code so that when another row is clicked, the currently open row collapses?
Here is the table:
<table class="tb">
<tbody>
<tr class="parent"><td>Click here</td></tr>
<tr><td>Hidden row</td></tr>
<tr class="parent"><td>Or click here</td></tr>
<tr><td>Another hidden row</td></tr>
</tbody>
</table>
And here is the jQuery:
$('table.tb').each(function() {
var $table = $(this);
$table.find('.parent').click(function() {
$(this).nextUntil('.parent').toggle();
});
var $childRows = $table.find('tbody tr').not('.parent').hide();
});
Upvotes: 1
Views: 2271
Reputation: 104785
Change your function to hide each parent on click
$(".parent").children("td").click(function() {
$(".parent").next("tr").hide();
$(this).parent().next("tr").show();
});
Upvotes: 2