Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: select next div outside of parent div

Ok, I feel like I"m missing something really simple but here goes:

This code works great, exactly the way I want it to. You click the link and the next div is revealed (There are about 10 of these on a page)

    $('a.addtask').click(function(){
      $(this).next('.field').slideToggle();
    return false;
    });

  <div class="field">
   Some content
  </div>

  <a href="#" class="addtask">Add a task</a>

  <div class="field" style="display:none;">
    some other content
  </div>

What I want to do however is change the HTML like so (link inside the div):

<div class="field">
   Some content
   <a href="#" class="addtask">Add a task</a>
  </div>


  <div class="field" style="display:none;">
    some other content
  </div>

^^Which doesn't work properly anymore.

What do I need to change in my jquery to make this work? I've been googling for a while now and a number of solutions in similar posts dont seem to be working.

like image 570
Clayton Correia Avatar asked Jul 22 '12 23:07

Clayton Correia


1 Answers

$(".addtask").parent().next(".field") should work

like image 186
Naren Avatar answered Oct 06 '22 10:10

Naren