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

198
Visualizações
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 à 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