bonaca bonaca - 6 months ago 22
jQuery Question

next("some class") doesn't work

HTML

<div class="linksrtitle">Lorem ipsum</div>
<div class="linksrspace"></div>
<div class="linksrwrap"> // this div should be slided
<a class="linkr" href="volim-da-stoje.php">Lorem ipsum</a>
<a class="linkr" href="ova-salate-je-umrla.php">Lorem ipsum</a>
<a class="linkr" href="nova-rasa.php">Lorem ipsum</a>
</div>
<div class="linksrspace"></div>


JS

$(".linksrtitle").click(function(){
$(this).next(".linksrwrap").slideToggle(); // doesn't work
});


Why this click event doesn't work. Console is empty.

Answer

Use nextAll()(with first(), if multiple sibling with same class are there) to get that. next() only select immediate following sibling.

$(".linksrtitle").click(function() {
  $(this).nextAll(".linksrwrap").slideToggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="linksrtitle">Lorem ipsum</div>
<div class="linksrspace"></div>
<div class="linksrwrap">// this div should be slided
  <a class="linkr" href="volim-da-stoje.php">Lorem ipsum</a>
  <a class="linkr" href="ova-salate-je-umrla.php">Lorem ipsum</a>
  <a class="linkr" href="nova-rasa.php">Lorem ipsum</a>
</div>
<div class="linksrspace"></div>