Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

117
Vistas
toggleClass and remove class from all other elements

How can I toggleClass and remove class from all other elements? Consider a div that contains a tags: html:

<div class="size">
   <a href="">blahblah</a>
   <a href="">blahblah</a>
</div>

jQuery:

 $(".size a").click(function(){
 $(this).toggleClass('checked');
 if($(".size a").hasClass('checked')){
     $(this).removeClass('checked');
  }
 })

I want to add class "cheched" to element and remove the class "ckeched" from other elements that have class "checked" . My code remove all classes. How can I add specific class and remove other element's class with click? Thanks in advance

8 months ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This will do it

 $(".size a").click(function(){
    $('.size a.checked').not(this).removeClass('checked');
    $(this).toggleClass('checked');
 })

Update

Alternatively you could do

 $(".size").on('click','a', function(){
    $(this).toggleClass('checked').siblings().removeClass('checked');
 })
8 months ago · Santiago Trujillo Denunciar

0

Only You need to Use it. :)

<script>
    $(document).ready(function () {
        $('.demo_class').click(function () {
            $(this).toggleClass('active').siblings().removeClass('active');
        });
    });
</script>
8 months ago · Santiago Trujillo Denunciar

0

So late but its my answer

$(".size").click(function(){ $('.size').removeClass('checked') //Clear all checked class on .size $(this).addClass('checked') //Add checked class to current clicked .size })

Its cleaner,powerfull and safe.

8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.