‹ JavaScript

classList

Permite agregar, quitar o alternar clases CSS de un elemento sin manipular directamente el atributo class como texto.

Vista previa

Codigo

ejemplo.html
<button id="boton" style="padding:8px 16px;">Alternar clase</button>
<p id="caja" style="margin-top:8px;padding:10px;">Caja</p>
<style>
  .resaltada { font-weight: 800; text-decoration: underline; }
</style>
<script>
  document.getElementById("boton").addEventListener("click", () => {
    document.getElementById("caja").classList.toggle("resaltada");
  });
</script>