Struggling to toggle a DIV (hide and show) when the 'commentDIV' button is pressed? Despite my efforts, I haven't been able to make it work so far. Below is my HTML code. My goal is to hide/show a specific DIV block only when its associated button is pressed. I'm uncertain how to link the button to the DIV properly. Currently, my code can hide the DIV but fails to show it again. It seems to only work for the first commentDIV and not for others...
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.commentDIV').hide();
$('.commentButton').click(function () {
$(this).next('.commentDIV').show();
});
$('.closeButton').click(function () {
$(this).next('.commentDIV').hide();
});
});
</script>
</head>
<body>
<div id="abc" style="border:2px dashed;width:600px;background:white;">
<table border="2px">
<tr>
<td style="text-align:center;">
<div style="border:2px solid;margin:2px;padding:2px;width:60px;border-radius:10px;moz-border-radius:10px;background:#39C46E;height:50px;text-align:center;line-height: 25px;white-space:nowrap;color:white;display:inline-block;">
100 %<br>Success</div>
<span style="color: green">✔ Tested</span>
<span>24/4/2013</span>
</td>
<td>
<span id="couponTitle" >FLAT 25% OFF ON ALL MITASHI TABLETS</span><br>
<span id="couponCode" style="display:inline-block;border:2px dashed;padding:5px;margin:5px;background:#39C46E;color:white">GCKSPNHIAC18</span>
<br>
<span style="font-size:10pt;">Get the Samsung Galaxy Ace Duos S6802 (black) listed on the landing page for Rs. 8495 (Originally for Rs. 9990). Offer valid till 25th April midnight.</span>
</td>
<td style="text-align:center;width:100px">
<span style="font-size:10pt;">Did this coupon work for you?</span>
<br>
<button type="button" style="border-radius:5px;-moz-border-radius:5px;background:#39C46E;color:white;width:50px;">Yes</button>
<button type="button" style="border-radius:5px;-moz-border-radius:10px;background:#FF511C;color:white;width:50px;">No</button>
</td>
</tr>
<tr>
<td colspan="3" style="text-align:right;">
<button type="button" class="commentButton" style="border-radius:5px;-moz-border-radius:10px;color:white;width:100px;">Add comments</button>
<button type="button" class="closeButton" style="border-radius:5px;-moz-border-radius:10px;color:white;width:100px;">Close comments</button>
<div class="commentDIV" style="height:300px;overflow-x:hidden;">
<table border=".1em">
<tr>
<td style="text-align:center;width:100px;height:100px;border-radius:5px;-moz-border-radius:5px;background: url(avatar.jpg) no-repeat center"></td>
<td style="width:600px;">
<div style="text-align:left;border-radius:5px;-moz-border-radius:5px;width:100%;overflow: auto;">Nice coupons
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
Update: added a fiddle here
I have updated the code to include another button for closing commentDIV
The JavaScript now includes:
$(document).ready(function () {
$('.commentDIV').hide();
$('.commentButton').click(function () {
$(this).next('.commentDIV').show();
});
$('.closeButton').click(function () {
$(this).next('.commentDIV').hide();
});
});
Also, there's a new button:
<button type="button" class="closeButton" style="border-radius:5px;-moz-border-radius:10px;color:white;width:100px;">Close comments</button>
Despite these changes, the functionality isn't working as expected. Any ideas why?