How to toggle class
- Página anterior Cambiar texto
- Página siguiente Agregar clase
Use JavaScript to add and remove class names between elements.
Toggle class
Step 1 - Add HTML:
When adding the class name to id="myDIV"
toggle between div elements (in this example, we use a button to toggle the class name).
<button onclick="myFunction()">Try it</button> <div id="myDIV"> This is a DIV element. </div>
Step 2 - Add CSS:
Add a class name to toggle:
.mystyle { width: 100%; padding: 25px; background-color: coral; color: white; font-size: 25px; }
Step 3 - Add JavaScript:
obtain id="myDIV"
the <div> element, and in "mystyle"
Toggle classes between:
function myFunction() { var element = document.getElementById("myDIV"); element.classList.toggle("mystyle"); }
Related pages
Tutorial:¿Cómo agregar una clase
Tutorial:¿Cómo eliminar una clase
Manual de referencia:Atributo classList del elemento DOM HTML
- Página anterior Cambiar texto
- Página siguiente Agregar clase