bonaca bonaca - 1 year ago 85
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 Source

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>