Only toggle certain elements based on class

Casey Crookston

I have a simple table:

<table>
    <tr class="header">
        <th>
            ....
        </th>
    </tr>
    <tr class="level2">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
    <tr class="level2">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
</table>

This works great to toggle everything from one "header" to the next.

$('.header').click(function () {
    $(this).nextUntil('tr.header').toggle();
});

But what I can't figure out how to do is to ONLY toggle the elements with class of "level2" and leave "level3" hidden.

I've been playing around for a while with .toggleClass() and .netAll() but I am not getting it.

epascarello

Use filter to select the items

$('.header').click(function () {
    var trs = $(this).nextUntil('tr.header')
    trs.filter('.level2').toggle();
    trs.filter('.level3').hide();
});
.header {
  background-color: green;
}

.level2 {
  background-color: yellow;
}

.level3 {
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <tr class="header">
        <th>
            ....
        </th>
    </tr>
    <tr class="level2">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
    <tr class="level2">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
    <tr class="level3">
        <th>
            ....
        </th>
    </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Toggle Class based on ID on hover with other elements

Javascript Toggle Active Class Only on Specific Elements?

Only print elements with a certain class in nested divs

Using Vue 3 to toggle class of child elements based on dynamic props

Toggle elements if they have a class

Toggle class on 2 elements

CSS font family only apply to certain elements within the same class

scan values of certain class elements on page load and run a function to several elements with certain values only

Python / Selenium only print if class has a certain value + only print certain elements in html

Overflow only certain elements

CSS/SASS Select elements based on class, but exclude certain elements with same class

toggle class on multiple elements react

toggle class based on accordion visibility

Toggle Class Only in One Parent

React: how to toggle css class in a component based on 3 different state elements

SwiftUI allow Toggle only on certain conditions

Run/Toggle command for certain filetypes only

Determine if elements of a certain class have a certain text

Count elements not containing certain class

Toggle elements based on the selected row in the table in vuejs

ClassList toggle works only for some elements

How to trigger toggle() only once on all elements?

Can I write a CSS code for elements NOT having a certain class, but only style?

Implement 'toggle class' functionality on a list of elements with React

Toggle class with ng-click on several elements

how to toggle class for multiple elements in vue js

How to toggle all .class elements in the parent container

Toggle a class on and off between two elements

how to toggle active class in 2 elements react