Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select next div?

I have this part many times in a page:

<a class="showComment" style="cursor: pointer;"><i class="icon-comment"></i> Views</a>
<br />
<br />
<div class="writeComment" style="height: auto; width: 700px;" dir="ltr" hidden="hidden">
</div>

I am now writing code in a js file for a.showComment click event.Now I want to select next div.writeComment.How to select it?

like image 470
bbb Avatar asked Jun 11 '13 15:06

bbb


2 Answers

In the variable nextDiv you can do whatever you want
Using .nextAll() method allows us to search through the successors of these elements in the DOM tree and construct a new jQuery object from the matching elements.

using .next() instead you search after the DOM element that you have clicked try this:

 $('.showComment').click(function(){
        var nextDiv = $(this).nextAll("div.writeComment");
    });

Or

$('.showComment').click(function(){
            var nextDiv =  $('.showComment').next().find('.writeComment')
        });

Or

$('.showComment').click(function(){
        var nextDiv = $(this).next("div.writeComment");
    });
like image 196
Alessandro Minoccheri Avatar answered Sep 30 '22 07:09

Alessandro Minoccheri


next() didnt work.

nextAll() was for all of .writeComment elements that were after my .showComment element.But I found the issue.The following code made it.

$('.showComment').click(function(){
     $(this).nextAll(".writeComment").first();
});
like image 44
bbb Avatar answered Sep 30 '22 08:09

bbb