I've got the following HTML and because I'm using Wordpress, I cannot change the class for each div.
<div class="show_hide">content1</div>
<div class="extended">extension of content 1</div>
<div class="show_hide">content2</div>
<div class="extended">extension of content 2</div>
My jQuery script is as follows:
<script type="text/javascript">
$(document).ready(function(){
$(".sliding").hide(0);
$(".show_hide").show(0);
$('.show_hide').click(function(){
$(".sliding").slideToggle(0);
});
});
</script>
Now, when clicking on the show_hide div, both div's with class "extended" show. I would like to show only the extension the div clicked on. Can anyone help me with this?
If the .extended
element is the immediately following sibling of the .show_hide
element, you can use next
:
$('.show_hide').click(function(){
$(this).next().slideToggle(0);
});
If there are other elements in between, you can use nextAll
, and then reduce the selection to the first match (with eq
):
$('.show_hide').click(function(){
$(this).nextAll('.extended').eq(0).slideToggle(0);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With