2

I have an element :

...
<li class="products active"><a href="" title="Title">test</a></li>
...

I want to access this "li" tag and swap the "active" class to "past" class so the final result would be :

...
<li class="products past"><a href="" title="Title">test</a></li>
...

What is the easiest / efficient way to achieve this using jQuery ?

2
  • 1
    Why is it a "fake" class? At most there are pseudo classes, but no fake classes. If you mean that the class has no CSS rule, then it is still a normal class (with no CSS rules ;)). Commented Jan 6, 2011 at 15:34
  • Just because I saw an edit that included pseudo class: These classes are well defined. Every class that a user can create is just normal class. Nothing else. @jakemcgraw: Good edit. Commented Jan 6, 2011 at 15:40

6 Answers 6

5

It is much simpler to use toggleClass(), assuming the element already has one or the other.

Just give it both class names:

$('.active').toggleClass('past active');

If it has active (which it will since that is how it is being selected), its active class will be removed, and the past class will be added.

If you're doing this inside a click handler, it might look like this:

$('.products').click(function() {
    $(this).toggleClass('past active');
});

Example: http://jsfiddle.net/TCvQU/

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

13 Comments

Nice. I didn't know you could pass multiple classes to toggleClass() in that manner.
I´m just guessing here, but because it also has another class, .products, I don´t think it´s either .active or .past; .past might be something like previously selected...
... But I do agree with @jsumners
@jeroen: It sort of seems that way, but that's not the way it works. The add/remove/toggleClass methods work on them as individual classes instead of the entire "class" attribute. I added an example to the answer. (Unless I'm misunderstanding what you meant.)
...here's an updated example showing that the products class stays in tact.
|
4
$("li.active").removeClass("active").addClass("past");

Comments

1

You'll want to modify the selector so it only grabs the correct li(s)...

$('li').removeClass('active').addClass('past');

Perhaps to $('li.active');

Comments

1

With your current markup you would do something like:

$('li.active').removeClass('active').addClass('past');

If you want to change a specific list item element, though, then you will need to identify it somehow. Maybe something like:

<li class="products active" id="item1">..</li>

And then you can do:

$('#item1').removeClass('active').addClass('past');

Comments

1
$('.active').addClass('past').removeClass('active')

2 Comments

This would involve looking up all elements that have the "active" class twice. Chaining the operations "addClass" and "removeClass" reduces the document queries to one.
Would be good to make use of method chaining, would mean you don't have to find .active twice :-)
-1

I agree with all three answers. Above with the expection that we do not know if your are running your JQuery pure or as part of a CMS which may require some tag changing.

2 Comments

That's not really an answer, is it?
Well how can you give him the correct answer if he is using smarty for php the each operation needs to have '$J' instead of just '$'. If he using some other type of framework it maybe something else instead of '$'. So instead of repeating the three possibilities with '$J' in each line of code I just put there maybe a need to change the starting tag.

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.