So I’ve got this nice little function to toggle the content of a table. The content is stored in a <div> with the class “hiddenDiv”. There is a <span> with the class “toggle” containing a link that when clicked opens the “hiddenDiv”. I want to apply the whole script to the entire <tr>, so that when you click anywhere inside the <tr> it shows the “hiddenDiv”. However, when I just change the class of the <tr> to “toggle” and delete the <span> class nothing happens.. Any help is appreciated!
$(".toggle").click(function () {
if ($(this).next().is(":hidden")) {
$(".hiddenDiv").hide();
$(this).next().slideDown("fast");
} else {
$(this).next().hide();
}
});
HTML:
<table width="96%" border="0" align="center" cellpadding="4">
<tr class="toggle" id="messageRow">
<td width="4%" valign="top">
<input type="checkbox" name="cb<?php echo $row['id']; ?>" id="cb" value="<?php echo $row['id']; ?>" />
</td>
<td width="20%" valign="top"><div class="message_name"><a href="../../pages/profile_page/profile_page.php?id=<?php echo $Sid; ?>"><?php echo $Fname.' '. $Lname ;?></a></div></td>
<td width="58%" valign="top">
<span style="padding:3px;">
<a class="<?php echo $textWeight; ?>" id="subj_line_<?php echo $row['id']; ?>" style="cursor:pointer;" onclick="markAsRead(<?php echo $row['id']; ?>)"><?php echo stripslashes($row['subject']); ?></a>
</span>
<div class="hiddenDiv" id="hiddenDivId"> <br />
<?php echo stripslashes(wordwrap(nl2br($row['message']), 54, "\n", true)); ?>
<br /><br /><a href="javascript:toggleReplyBox('<?php echo stripslashes($row['subject']); ?>','<?php echo $my_uname; ?>','<?php echo $my_id; ?>','<?php echo $Sname; ?>','<?php echo $fr_id; ?>','<?php echo $thisRandNum; ?>')">Reply</a><br />
</div>
</td>
<td width="18%" valign="top"><span style="font-size:10px;"><?php echo $date; ?></span></td>
</tr>
</table>
If the hidden div you are talking about is in a td in the tr with the “.toggle” class then the following should work:
But you can simplify it as follows:
See the
slideToggle()doco.EDIT: Just saw your updated question with the html. If your element has an id then it is more efficient to select by id rather than class, i.e.,
$("#hiddenDivId")rather than$(".hiddenDiv"). Same for$("#messageRow")rather than$(".toggle").Also, you shouldn’t use tables for non-tabular data.