Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Remove class from all div's except the one I clicked on

Tags:

html

jquery

I have some code working, that toggles the class "active" on and off when you click on a swatch-wrapper div.

However I would only like one of these div's to have the active class at a time. Right now I could click them all, and they would all have the active class. I'm not sure how to remove the class from all the other div's except for the one I just clicked on.

I tried to use .parent(), but it didn't seem to work / I don't think I was using it correctly.

HTML

<div class="variation_form_section">
  <div class="select">
    <div class="swatch-wrapper"></div>
    <div class="swatch-wrapper"></div>
    <div class="swatch-wrapper"></div>
  </div>
</div>

JS

<script>
$(document).ready(function(){
    $(".variation_form_section .select div").each(function() { 
      $(this).click(function() {
        if($(this).hasClass( "active" )){
            $(this).removeClass( "active" );
        }else{
            $(this).addClass( "active" );
        }
      });
    }); 
});
</script>
like image 344
KVDD Avatar asked Nov 21 '14 18:11

KVDD


People also ask

How do you remove a specific class from all elements?

To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element.

Which method is used to remove the class using jQuery?

The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.

What is the jQuery script to remove a class in an element?

removeClass() Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

Which jQuery method removes the selected item?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.


3 Answers

You can use not like this:

$(".variation_form_section .select div").click(function() { 
  $(".variation_form_section .select div").not($(this)).removeClass('active');
});
like image 110
Bhojendra Rauniyar Avatar answered Sep 19 '22 17:09

Bhojendra Rauniyar


Simply, remove active class from them all and then add active class to the clicked element.

$(document).ready(function(){
    $(".variation_form_section .select div").click(function() { 
      $('.variation_form_section .select div').removeClass('active');
      $(this).addClass('active');
    }); 
});

Demo: http://jsfiddle.net/conm54k2/

like image 25
Mateusz Nowak Avatar answered Sep 19 '22 17:09

Mateusz Nowak


You can try with .siblings() http://api.jquery.com/siblings/

$(document).on('ready', function(){
    $(".variation_form_section .select div").on('click', function() { 
        $(this).addClass('active').siblings().removeClass('active');
    }); 
});

demo: http://jsfiddle.net/conm54k2/4/

like image 38
Paúl Díaz Navarrete Avatar answered Sep 17 '22 17:09

Paúl Díaz Navarrete