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

192
Vistas
Javascript to change button text back and forth using bootstrap 4 & fontawesome icons

I'm using bootstrap 4 to create a button with a dropdown menu. The functionality I need is that the button when closed contains the word open and a fontawesome icon beside it. When opened it should say close with an icon beside it.

I'm currently trying this

function toggle() {
  const btn = document.body.querySelector("#dropdownMenu2");

  if (btn.innerText == "Open") {
    btn.innerHTML = "<i class='fa fa-plus-circle' aria-hidden='true'></i>" + "Open";
  } else {
    btn.innerHTML = "<i class='fa fa-minus-circle' aria-hidden='true'></i>" + "Close";
  }
}
.dropdown-toggle {
  border-radius: 0;
  border-style: solid;
  border-color: #9d2235;
  background-color: #9d2235;
  color: #fff;
  padding: 8px 20px;
}
<button class="dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="toggle()">
               <i class="fa fa-plus-circle" aria-hidden="true"></i>                
                Actions
</button>

The button changes to close on the first click but never changes back to open when clicked again.

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

0

instead of trying to alter the dom. Why dont you just add a second button with exact details and then just add same class on both the buttons and use jquery(since bootstrap 4 uses jquery) to hide and display other like following

$('.buttonClass').toggle();
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