Jeroen Bellemans
Jeroen Bellemans

Reputation: 2035

Find all next classes until class jquery

I have a table like this:

<table>
    <tr class="type_1">
        <td></td>
        <td></td>
    </tr>
    <tr class="type_1">
        <td></td>
        <td></td>
    </tr>
    <tr class="type_2">
        <td></td>
        <td></td>
    </tr>
    <tr class="type_2">
        <td></td>
        <td></td>
    </tr>
    <tr class="type_1">
        <td></td>
        <td></td>
    </tr>
</table>

Now, if I click .type_1 all .type_2, until the next .type_1, should toggle.

Here's what I've tried:

$(".type_1").click(function() {
    $(this).next(".type_2").toggle();
});

EDIT: I also tried with .nextAll() but this will toggle ALL .type_2 Only those after the clicked .type_1 should toggle.

But this only allows me to toggle the first encountered .type_2. How can I achieve this?

Upvotes: 1

Views: 162

Answers (1)

Ykaro Lemes
Ykaro Lemes

Reputation: 97

In this case you can try a nextUntil

https://api.jquery.com/nextUntil/

$(".type_1").click(function() {
    $(this).nextUntil(".type_2").toggle();
});

Upvotes: 2

Related Questions