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

197
Vistas
Dynamic change of html attribute values

There is a template for creating buttons, the number may be different. It is necessary that each button opens a block when pressed. I tried using JS scripts, they correctly set the id of the drop-down element, but I can't change the link, as a result, either all the elements open, or all the buttons open only one section.

<a class="btn btn-warning shadow-sm mb-3" data-bs-toggle="collapse" href="#" data-bs-target="#target">{{profile.profile}}</a>
<div class="collapse target" id="target">
  <div class="card card-body"><a class="btn btn-warning shadow-sm mb-3">Some text1</a></div>
  <div class="card card-body"><a class="btn btn-warning shadow-sm mb-3">Some text1</a></div>
</div>

I want to change data-bs-target values, but after first element the script breaks and I get the same values

<script type="text/javascript">
  $('.collapse').each(function(index) {
    // получаем текущий id элемент
    var id = $(this).prop('id');

    $(this).prop('id', id + index);

    $(this).parent().find('[href="#' + id + '"]').attr('href', '#' + id + index);
    $(this).parent().find('[data-bs-target="#' + id + '"]').attr('data-bs-target', '#' + id + index);

  });
</script>

The result:

<a class="btn btn-warning shadow-sm mb-3" data-bs-toggle="collapse" href="#" data-bs-target="#target0" aria-expanded="true">Some text</a>
<div class="target collapse show" id="target0" style="">
  <div class="card card-body"><a class="btn btn-warning shadow-sm mb-3">Some text1</a></div>
  <div class="card card-body"><a class="btn btn-warning shadow-sm mb-3">Some text1</a></div>
</div>


<a class="btn btn-warning shadow-sm mb-3" data-bs-toggle="collapse" href="#" data-bs-target="#target0" aria-expanded="true">Some text</a>
<div class="collapse target" id="target1">

Help me find mistakes pls

about 4 years ago · Juan Pablo Isaza
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