Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Multiple bootstrap collapses and jquery/javascript efficiency in toggling a display

I often use a caret icon to show if a collapse in Bootstrap 5 is open or closed.

For example, I'll have some html like this:

<h4 class="border" role="button" data-bs-toggle="collapse" href="#plinks">
  <i id="plinks-closed" class="fa fa-caret-right"></i> 
  <i id="plinks-open" style="display:none;" class="fa fa-caret-down"></i> 
  Plinks
</h4>   

and some js like this to switch out the caret icon when the collapse is opened or closed:

$('#plinks').on('show.bs.collapse', function () {
  $( "#plinks-closed").hide();
  $( "#plinks-open").show();
});
$('#plinks').on('hide.bs.collapse', function () {
  $( "#plinks-closed").show();
  $( "#plinks-open").hide();
});

Sometimes I have a lot of these and I use a lot of different IDs and js sections to make them work, and all those js sections for each ID seems kind of messy and inefficient.

What I'm looking for is a more universal non-id specific way to take the right-caret and swap it out for the left-caret in an element that I assign .caret-collapse. It would automatically populate the "closed caret" upon load then automatically swap that out for the "open caret" when the collapse is opened.

I'm imagining the html would look something like:

<h4 class="border caret-collapse" role="button" data-bs-toggle="collapse" href="#plinks">
  <span class="caret-collapse-container"></span>
  Plinks
</h4> 

As for the js, I'm guessing it would check for the click on the .caret_collapse and then do some kind of "child" lookup for the .caret-collapse-container and add/remove the icon code, but I've had no luck making something like that happen and thought I'd ask here instead of getting more frustrated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're making it much too complicated. For one thing, avoid IDs in CSS and JS whenever possible. They don't allow reusability. For another, you can just target classes for toggle from the collapse element.

This demo isn't working, possibly due to faulty markup, but it gives you the idea.

const myEls = document.querySelectorAll('[data-bs-toggle="collapse"]');

myEls.forEach(el => {
  el.addEventListener('shown.bs.collapse', function() {
    el.find('i').classList.toggle('fa-caret-right');
    el.find('i').classList.toggle('fa-caret-left');
  });
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>

<h4 class="border" role="button" data-bs-toggle="collapse" href="#plinks">
  <i class="fa fa-caret-right"></i> Plinks
</h4>

<div class="collapse" id="plinks">
  <div class="card card-body">
    Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda