This should be a nice easy one for someone – I have a table row that slideToggles when an image is clicked. As the table is dynamic, there could be any number of rows so I have had to identify the areas to show/hide by classname.
Not a problem. However, I only ONE instance of the extrainfo div to be shown at once. As one is shown, any already visible should be hidden:
EDIT: Here is a fiddle: http://jsfiddle.net/shpsD/
Added HTML below.
var toggleSpeed = 300;
var expandImg = "../Images/expand.png";
var collapseImg = "../Images/collapse.png";
$(".moreless").click(function () {
var detailsRow = $(this).parent().parent().next();
detailsRow.find('.extrainfo').slideToggle(toggleSpeed);
if ($(this).attr('src') == collapseImg) {
$(this).attr('src',expandImg);
$(this).closest('tr').removeClass('highlight_row');
}
else {
$(this).attr('src',collapseImg);
$(this).closest('tr').addClass('highlight_row');
}
});
});
–
<table>
<tr>
<th>Header</th>
<th></th>
</tr>
<tr>
<td>row 1</td>
<td><img src="expand.png" class="moreless" /></td>
</tr>
<tr>
<td colspan="2">
<div class="extrainfo">
EXTRA INFO!!
</div>
</td>
</tr>
<tr>
<td>row 2</td>
<td><img src="expand.png" class="moreless" /></td>
</tr>
<tr>
<td colspan="2">
<div class="extrainfo">
EXTRA INFO!!
</div>
</td>
</tr>
</table>
First hide the elements:
You could also use the
:visibleselector, if you want, though it doesn’t necessarily make things any faster, but perhaps a little more understandable:And then show:
JS Fiddle demo.
Edited with regards to the comment left by the OP (below):
I’m not sure, this was a pretty quick look once I’d finished work, but I might be over-doing things, but the following seems to work as you’d require:
JS Fiddle demo.
(I’ll add references and answer further questions once I’ve eaten…sorry!)
References:
attr().closest().find().slideDown().slideUp().:visibleselector.