0

I'm trying to add a class to an element using jQuery. If the user clicks on an element it adds a class, which I can do, but I want only 1 instance of that class to be active so if the user clicks on something else that class is then removed from the previous element and added to the current one.

https://jsfiddle.net/gsuxjce2/3/

<div class="row">
  <div class="col">
    <div class="box">

    </div>
  </div>
  <div class="col">
    <div class="box">

    </div>
  </div>
  <div class="col">
    <div class="box">

    </div>
  </div>
  <div class="col">
    <div class="box">

    </div>
  </div>
</div>

.col {
  width: 25%;
  float: left;
}

.box {
  width: 50px;
  height: 50px;
  background-color: red;
  cursor: pointer;
}

.added {
  background-color: blue;
}

 $('.box').click(function() {
        $(this).toggleClass('added');

    if ( $(this).hasClass('added'); ) {
                    $(this).removeClass('added');
    } else {
            $(this).toggleClass('added');
    }

 });
1
  • 1
    $('.foo').removeClass('foo') Commented Feb 22, 2016 at 16:15

3 Answers 3

3

Remove the .added class from all .box elements first, then just add it to the one clicked on with this:

 $('.box').click(function() {
     $('.box').removeClass('added');
     $(this).addClass('added');
 });

 $('.box').click(function() {
   $('.box').removeClass('added');
   $(this).addClass('added');
 });
.col {
  width: 25%;
  float: left;
}
.box {
  width: 50px;
  height: 50px;
  background-color: red;
  cursor: pointer;
}
.added {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row">
  <div class="col">
    <div class="box">

    </div>
  </div>
  <div class="col">
    <div class="box">

    </div>
  </div>
  <div class="col">
    <div class="box">

    </div>
  </div>
  <div class="col">
    <div class="box">

    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Love how simple this is! Is it the 'this' that's stopping all the other ones from also adding the class?
The this above refers to the .box element being clicked on.
2

$('.added').removeClass('added');

inside your click event before everything.

1 Comment

Thanks - this is the one I saw first and tried even though j08691 answered the same as well
1

Just add this to the start of you click event:

jQuery(".added").each(function(i,el){
    jQuery(el).removeClass("added")
});

This removes all instances of the class, so that when you add one new, then that will be unique.

4 Comments

Any particular reason you included .each?
What does the i, el do?
To lejimmie: i is the current index in the array of elements that contain the class added and el is the current element. The el is simply a reference to an of the HTML element that contain the class. To James Thorpe: I was going for the most versatile solution, since that would be easiest for a green developer to extend from. Rodrigo Graça's solution though is more professional and a lot nicer to look at.
Thanks for the explanation. Noted :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.