Is there a way to ensure that only one table row expands at a time, causing the others to collapse? Any suggestions on achieving this? Here's the HTML code snippet:
<table class="table">
<tbody>
<tr class="parent" id="2479">
<td><span class="btn btn-default">expand</span></td>
</tr>
<tr class="child-2479 ">
<td>row 1</td>
<td>row 1</td>
<td>row 1</td>
<td>row 1</td>
<td>row 1</td>
<td>row 1</td>
</tr>
<tr class="parent" id="2800">
<td><span class="btn btn-default">expand</span></td>
</tr>
<tr class="child-2800">
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
<td>row 2</td>
</tr>
</tbody>
</table>
Here is the jQuery code snippet used to achieve this functionality:
$(function() {
$('tr.parent td span.btn')
.on("click", function(){
var idOfParent = $(this).parents('tr').attr('id');
$('tr.child-'+idOfParent).toggle('slow');
});
$('tr[class^=child-]').hide().children('td');
});
Check out the live demo here.