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

469
Visualizações
Javascript Changing button inner text without chaning the icon

I created the following button, and want to change the inner text "test" to other word.

            <button type="button" id="test"><i class="fa-2x fa-regular fa-floppy-disk"></i>test</button>

I tried below method it can change the word but also deleted the icon.

$('button#bkmarktest').on('click', function(e){
e.preventDefault(); 
$(this).text("other");
});

Is there any way that can only change the word, but keeping the icon, thanks!

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

0

What .text() method does is that it overwrites not only text but HTML as well. The easiest thing to do is move your text inside the <i> so it doesn't get overwitten only what's inside of it. Review the example below and notice that the icon remains -- it's because it's actually a CSS pseudo-element and is always ignored by methods and functions dealing with the DOM.

$('.test').on('click', function() {
  $(this).find('i').text(' IT WORKS!')
});
<link href='https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css' rel='stylesheet'>

<button class='test' type='button'>
  <i class="fa fa-star-o"> TEST</i> 
</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

I haven't use jQuery since very long so here's an example in vanilla javascript.

const btn = document.querySelector('#test');

btn.addEventListener('click', (e) => {
  e.preventDefault();
  // selecting span tag -> you can also give it a specific id or class if you want
  btn.children[1].innerText = 'other';
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet"/>
<button type="button" id="test">
  <i class="fa-2x fa-regular fa-floppy-disk"></i>
  <span>test</span>
</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

like @Kunal Tanwar said, wrapping the text in a span solves it and using Jquery this is my approach.. assuming the button has an id of test

using the .find() method to find the span child

<button type="button" id="test">
  <i class="fa-2x fa-regular fa-floppy-disk"></i>
  <span>test<span>
</button>

$('#test').on('click', function(e){
  e.preventDefault(); 
  $(this).find('span').text("other");
});

and another option would be to use the decendant selector since the span is a direct child of the button

$('#test').on('click', function(e){
  $('#test > span').text('other');
});
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