How can I get the open/close (i.e., up/down arrows) to work independently in the following code? Now, they operate in tandem which is misleading. When I ‘open’ Product A it should then display the up or close arrow which it does but so does Product B which hasn’t been ‘opened’ yet.
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
.productDetails {
display: none;
}
</style>
</head>
<body>
<table border="1">
<tr>
<td><a href="#" class="expandProductDetails">Product A <span>↓</span><span style="display: none;">↑</span></a></td>
</tr>
<tr class="productDetails">
<td><strong>Product Philosophy</strong> Aliquam eu velit nibh. In eleifend convallis ante, sit amet semper arcu lobortis vitae.</td>
</tr>
<tr>
<td><a href="#" class="expandProductDetails">Product B <span>↓</span><span style="display: none;">↑</span></a></td>
</tr>
<tr class="productDetails">
<td><strong>Product Philosophy</strong> Nunc ac nisi vel leo iaculis feugiat. Quisque blandit tempor vestibulum.</td>
</tr>
</table>
<script>
$('.expandProductDetails').click(function() {
$(".expandProductDetails span").toggle();
$(this).closest("tr").next().slideToggle("slow");
});
</script>
</body>
</html>
Working (somewhat) example: http://jsfiddle.net/stulk/mqjuR/
Change this:
to this:
$(".expandProductDetails span")targets every element that has the classexpandProductDetails(and subsequently selects those elements’ childspanelements. You want to target the childspanelements of only the element that was clicked which, in the context of your click event, isthis.Updated example