How to get an element with a class after the current one in jQuery?
Here is an example html code:
<div id="current_element">Current element</div>
Many unknown tags...
<div class="target">This is target element</div>
Many other tags...
Note. The target element and the current element may not be under the same parent, so I cannot find it with .nextAll ('. Target'), right?
Is there a simple way to find it? Thank!
You html in the comment is different from the one asked in the question
<div id="area1">
<div id="current_element">Current</div>
</div>
<div id="area2">
<div class="target">Target</div>
</div>
What I would do is wrap their div:
<div id="mainparent">
<div id="area1">
<div id="current_element">Current</div>
</div>
<div id="area2">
<div class="target">Target</div>
</div>
<div>
Then I will return and find another child:
//this = .current_element
var $target = $(this).closest("#mainparent").find(".target");
Hope this helps!