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

229
Vistas
How to change a FontA. icon to another one that has the same class name

I've been trying to a FontA. icon to another one that has the same class name. What I tried was to add a class name named "active". Because there were only two icons to change back and forth, I thought HTML can show an icon I want by checking if the icon has "active". When the icon has "active", then I want to display it, but if not, I don't need to. I think the problem is that they have the same class names.

enter image description here

The JS displayed this problem. enter image description here

Also, there is only one way to hide an element?? Even if I added these below,

opacity: 0; 
visibility: hidden;

I saw blank space on a page. I didn't like it, so I wrote display: none; instead. But, I just wanna know there are some other ways to hide an element. enter image description here

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

0

I won't understand your query well, But as much as I understand you want to change the icon of using js.
It can be done using CSS only.
"Just use className.active to simply change the styling of the class Element. 'Eg:- .bi-list.active' It will change the style only on the class which has .active as its class item."

For CSS Only having Same Cl

let icon1 = document.querySelectorAll('.bi')[0];
    let icon2 = document.querySelectorAll('.bi')[1];
    function changeIcon(){
      icon1.classList.toggle('active'); 
      icon2.classList.toggle('active'); 
    }
i{
  display:none;
}
i.active{
  display:inline;
}
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.1/font/bootstrap-icons.css">
</head>
<body>
<span>
<i class="bi bi-list active" onclick="changeIcon()"></i>
<i class="bi bi-x-lg" onclick="changeIcon()"></i>
</span>
</body>

ass name


For Javascript:

let icon = document.querySelector('.bi-list');
function ChangeIcon(){
  if(icon.classList.item(1) == "bi-list"){
    icon.classList.remove("bi-list");
    icon.classList.add("bi-x-lg");
  }
  else{
    icon.classList.remove("bi-x-lg");
    icon.classList.add("bi-list");
  }
}
.menu .bi{
  color:blue;
  display:inline;
}
<html>
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.1/font/bootstrap-icons.css">
  </head>
  <body>
    <span class="menu">
    <i class="bi bi-list" onclick="ChangeIcon()"></i>
      <!--<i class="bi bi-list active" onclick="ChangeIcon()"></i>
      <i class="bi bi-x-lg"></i>-->
    </span>
  </body>
</html>

And For the same classes, You can use document.querySelectorAll('.bi')[0] for accessing 1st element and ('.bi')[1} for accessing 2nd element.

If you want me to change anything in this code, Let me know!

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