Reputation: 1670
I have an element that I am using as an expander. When I click on it uses the toggle function to show/hide the expander-container
element.
<script type="text/javascript">
$(document).ready(function () {
$(".expand-icon").click(function () {
$(".expander-container").toggle(500);
});
});
<img class="expand-icon" src="./Content/Icons/toggle-expand-icon.png" alt="some_text" />
<div class="expander-container" >
...
</div>
How can I target only the next occurence of the expander-container
element without targeting all of them on the page?
Upvotes: 0
Views: 53
Reputation: 20830
Use .next()
$(document).ready(function () {
$(".expand-icon").click(function () {
$(this).next(".expander-container").toggle(500);
});
});
Here is the working demo : http://jsfiddle.net/XLNM5/
Upvotes: 1
Reputation: 27845
I think you should go for go for
$(".expand-icon").click(function () {
$(this).next('.expander-container').toggle(500);
});
Upvotes: 2