1

I have a table formatted like that following:

<table>
    <tbody>
        <tr class="node">
            <td onclick='toggleDesc(this.parentNode);'>blah</td>
        </tr>
        <tr class="subnode">
            <td>sblah</td>
        </tr>
        <tr class="subnode">
            <td>sblah</td>
        </tr>
        <tr class="subnode">
            <td>sblah</td>
        </tr>
        <tr class="node">
            <td onclick='toggleDesc(this.parentNode);'>blah</td>
        </tr>
        <tr class="subnode">
            <td>sblah</td>
        </tr>
        <tr class="subnode">
            <td>sblah</td>
        </tr>
    </tbody>
</table>

I also have a function that toggles the display of the .subnode classes based on the TD of the .node above it. The function looks like this:

function toggleDesc(item) {
    var descRow = item.nextElementSibling;
    $(descRow).toggleClass("descDisplay");
}

However, the function above only toggles the display of the first .subnode it encounters. Does anyone know how to .toggleClass for each .subnode after .node until the next .node is hit?

1 Answer 1

2

Use nextUntil():

$(item).nextUntil('tr.node').toggleClass("descDisplay");

See Documentation

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, this will do. Didn't know of .nextUntil method. Thanks!
I ended up using something like $(item).nextUntil(".node, .subnode").toggleClass("descDisplay").
.nextUntil('tr.node') will do in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.