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

335
Visualizações
jQuery: change link text and icon on click

So I am trying to change the the icon of my link for collapse section alongside with the text of the link.

Here is my code:

<a href="#collapse", class="btn", role="button", data-bs-toggle="collapse" id="btn-collapse">
    <i class="material-icons" style="vertical-align: middle;">expand_more</i>
    <label style="vertical-align: middle;">PRIMARY TEXT</label>
</a>

<!--CONTENT-->

<script>
    $(document).ready(function(){
        $('#btn-collapse').on('click', function () {
            var text=$('#btn-collapse').text();
            if(text === "SECONDARY TEXT"){
                $(this).text('PRIMARY TEXT');
                $(this).find("i").html("expand_more");
            } else{
                $(this).text('SECONDARY TEXT');
                $(this).find("i").html("expand_less");
            }
        });
    });
</script>

I've been trying to find a solution for this for a while now, but nothing seems to work.. Text changes fine, but the icon completely vanishes on click. I don't think it is a major issue, but there is something I don't get with this. Any help here how to adjust?

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

0

$(document).ready(function() {
  $('#btn-collapse').on('click', function() {
    var thisElement = $(this);
    var anchorLabelElement = thisElement.find('label');
    if (anchorLabelElement.text() === "SECONDARY TEXT") {
      anchorLabelElement.text('PRIMARY TEXT');
      thisElement.find("i").text('expand_more');
    } else {
      anchorLabelElement.text('SECONDARY TEXT');
      thisElement.find("i").text("expand_less");
    }
  });
});
<link href="https://cdn.jsdelivr.net/npm/material-design-icons-iconfont@6.7.0/dist/material-design-icons.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#collapse" , class="btn" , role="button" , data-bs-toggle="collapse" id="btn-collapse">
  <i class="material-icons" style="vertical-align: middle;">expand_more</i>
  <label style="vertical-align: middle;">PRIMARY TEXT</label>
</a>

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