[Solved] jquery, find next element by class

How can i find the next element by class.

i tried with $(obj).next('.class'); but this returns classes only in $(obj) parent.
I need to take the next element anywhere throughout the code by class name.
Because my code looks like

<tr><td><div class="class">First</div></td></tr>
<tr><td><div class="class">Second</div></td></tr>

Is this possible?

Enquirer: Alex


Solution #1:

In this case you need to go up to the <tr> then use .next(), like this:


Or if there may be rows in-between without the .class inside, you can use .nextAll(), like this:

Respondent: Nick Craver

Solution #2:

To find the next element with the same class:

$(".class").eq( $(".class").index( $(element) ) + 1 )
Respondent: plavozont

Solution #3:

You cannot use next() in this scenario, if you look at the documentation it says:

Next() Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling that matches the selector.

so if the second DIV was in the same TD then you could code:

// Won't work in your case

But since it’s not, I don’t see a point to use next(). You can instead code:


Respondent: Morteza Manavi

Solution #4:

Given a first selector: SelectorA, you can find the next match of SelectorB as below:

Example with mouseover to change border-with:

$("SelectorA").on("mouseover", function() {
    var i = $(this).find("SelectorB")[0];
    $(i).css({"border" : "1px"});

General use example to change border-with:

var i = $("SelectorA").find("SelectorB")[0];
$(i).css({"border" : "1px"});
Respondent: TagFolks

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy