Richard Knop
Richard Knop

Reputation: 83745

Get next element with the same class

I use a click event for an element with certain class. Inside the click event function I need to get the next element on page with the same class. I tried:

$('.class').click(function() {
    var $next = $(this).next('.class')  
});

And:

$('.class').click(function() {
    var $next = $(this).parent().parent().parent().next('.class')  
});

The .class is a div inside td in a table, that's why three times parent().

My HTML is something like this (The .class is .drag):

<table cellspacing="0" cellpadding="0" border="0" class="black8">
<tr class="trow1 drop trow1_over" 
    rel="0,1">
    <td>
            <div class="drop rootFolder" 
                 rel="0,1"></div>
    </td>
    <td width="100%" 
        class="folderListOnclick">

            <span>.. (koreňový adresár)</span>
    </td>
    <td>
    </td>
    <td>
    </td>
</tr><tr class="trow1">
    <td>
        <div class="drag drop ordinaryFolder" 
             rel="1,1" 
             style="width: 40px;">

        </div>
    </td>
    <td width="100%" 
        class="folderListOnclick" 
        rel="1">
            <span>aaa</span>
    </td>
    <td>
        <div class="button_mini folderEditOnclick" 
             rel="1">
                    <span></span>

        </div>
    </td>
    <td>
        <div class="button_mini folderDeleteOnclick" 
             rel="1">
                    <span><span>
        </div>
    </td>
</tr><tr class="trow1">
    <td>

        <div class="drag drop ordinaryFolder" 
             rel="19,1" 
             style="width: 50px;">
        </div>
    </td>
    <td width="100%" 
        class="folderListOnclick" 
        rel="19">
            <span>subaaa</span>
    </td>
    <td>
        <div class="button_mini folderEditOnclick" 
             rel="19">

                    <span></span>
        </div>
    </td>
    <td>
        <div class="button_mini folderDeleteOnclick" 
             rel="19">
                    <span><span>
        </div>
    </td>
</tr>
</table>

Upvotes: 9

Views: 5775

Answers (4)

Ren&#233; Orozco
Ren&#233; Orozco

Reputation: 51

I know, it's been a lot of time from this question, but... I find another way to do this in one line:

$('.class').on('click', function(){
    const next = $(this).nextAll('.class').eq(0)
    ...
})

$('a').on('click', function(e){
    e.preventDefault()
    $('.class').removeClass('next')
    $(this).nextAll('.class').eq(0).addClass('next')
})
a {
  background-color: rgba(0,0,0,0.1)
  display: block;
  padding: 4px 16px;
  width: 200px;
}
a.next {
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="class">Class 1</a>
<a href="#" class="noclass">Noclass 1</a>
<a href="#" class="class">Class 2</a>
<a href="#" class="noclass">Noclass 2</a>
<a href="#" class="noclass">Noclass 3</a>
<a href="#" class="class">Class 3</a>

Upvotes: 0

Nathan
Nathan

Reputation: 11159

How about this? You should check for off-by-one errors though, because I haven't tested it.

$('.class').click(function () {
  var index = $('.class').index($(this));
  var $next = $('.class').slice(index+1,index+2);
  ...
});

Upvotes: 5

Felix Kling
Felix Kling

Reputation: 817030

You can use a combination of slice(), and each():

var elements = $('.class');

elements.each(function(index, element) {
    $(this).click(function() {
        var $next = elements.slice(index+1, index+2);
    });
});

DEMO

Alternatively (though maybe less efficient) would be to get a reference to the common ancestor and use slice() and index():

$('.class').click(function() {
    var elements = $(this).closest('table').find('.class');
    var index = elements.index(this);
    $next = elements.slice(index + 1, index + 2);
});

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263117

You can try:

$(".drag").click(function() {
    var draggables = $(this).parents("table").find(".drag");
    var $next = draggables.filter(":gt(" + draggables.index(this) + ")").first();
});

Upvotes: 3

Related Questions