Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Multiple SlideToggle on one page, how to control each on independently

I'm setting up a few jquery slideToggle for a filter system I have and I'm trying to have them use the same classes. There will be about 5 of the same slide toggle on the page but when I close / open one of them, it closes them all and I'm wondering how I can adjust the the javascript so each one closes individually.

<div class="tc-facet-header tc-toggle-show">
        <h3>Header One</h3>
        <span><i class="bi bi-chevron-down"></i><i class="bi bi-chevron-up"></i></span>
    </div>
    <div class="tc-facet tc-facet-open">
        Fitler Content 1
    </div>

<div class="tc-facet-header tc-toggle-show">
        <h3>Header two</h3>
        <span><i class="bi bi-chevron-down"></i><i class="bi bi-chevron-up"></i></span>
    </div>
    <div class="tc-facet tc-facet-open">
        Fitler Content 2
    </div>

The JS I use is

    <script>
    $(document).ready(function(){
    var $content = $(".tc-facet").show();
    $(".tc-facet-header").on("click", function(e){
        $(this).toggleClass("tc-toggle-show");
        $(this).toggleClass("tc-toggle-hide");
        $content.slideToggle();
    });
    });

When I click on a h3 header of either one it will open / close both the .tc-facet divs instead of just the one i click on.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your current jquery code is targeting all .tc-facet that's why. You need to target each individual .tc-facet. Try this code

$(document).ready(function(){
    $(".tc-facet-header").on("click", function(e){
        $(this).toggleClass("tc-toggle-show");
        $(this).toggleClass("tc-toggle-hide");
        $(this).next(".tc-facet").slideToggle();
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="tc-facet-header tc-toggle-show">
    <h3>Header One</h3>
    <span><i class="bi bi-chevron-down"></i><i class="bi bi-chevron-up"></i></span>
</div>
<div class="tc-facet tc-facet-open">
    Fitler Content 1
</div>

<div class="tc-facet-header tc-toggle-show">
    <h3>Header two</h3>
    <span><i class="bi bi-chevron-down"></i><i class="bi bi-chevron-up"></i></span>
</div>
<div class="tc-facet tc-facet-open">
    Fitler Content 2
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

It's been a few years since I looked at any jquery code, but from what I can see, the reason you're seeing all of them toggle is that you're running the slideToggle function on the $content variable you set earlier which includes all of the matches at the time the variable is set.

If all you're trying to do is run the slideToggle function on the span when the heading is clicked, you can just do this:

$(document).ready(function() {
  $(".tc-facet-header").on("click", function(e) {
    $(this).next().slideToggle();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="tc-facet-header tc-toggle-show">
  <h3>Header One</h3>
  <span><i class="bi bi-chevron-down"></i><i class="bi bi-chevron-up"></i
      ></span>
</div>
<div class="tc-facet tc-facet-open">Fitler Content 1</div>

<div class="tc-facet-header tc-toggle-show">
  <h3>Header two</h3>
  <span><i class="bi bi-chevron-down"></i><i class="bi bi-chevron-up"></i
      ></span>
</div>
<div class="tc-facet tc-facet-open">Fitler Content 2</div>

As long as the span follows the heading, then the span will always be toggled.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda