Skip to main content

Bug Tracker

Side navigation

#1681 closed enhancement (fixed)

Opened September 19, 2007 04:40PM UTC

Closed December 25, 2008 08:12PM UTC

More powerful toggleClass()

Reported by: dimi Owned by: flesler
Priority: major Milestone: 1.3
Component: core Version: 1.2.1
Keywords: Cc: dimi, ecentinela, genezys
Blocked by: Blocking:

A lot of times you need to add/remove a class to an element based on a boolean. toggleClass() should take an optional second parameter that would control if the toggle will add or remove the class.

The added complexity would be absolutely minimal (a few more characters), but it would be really useful.

Attachments (2)
  • src-1681.diff (0.6 KB) - added by genezys August 15, 2008 10:51AM UTC.
  • test-1681.diff (0.8 KB) - added by genezys August 15, 2008 10:51AM UTC.
Change History (7)

Changed November 14, 2007 03:11AM UTC by brandon comment:1

need: ReviewTest Case

Could you provide a few examples of how this will improve your code?

Changed December 07, 2007 11:58AM UTC by genezys comment:2

I agree with this one, it would be nice to be able to add/remove a class based on a boolean value.

Here are some examples:

"item" is an object
"$item" is the jQuery object representing "item"
I want to add the class "Selected" to the UI when the object has the attribute "selected".

First version, very verbose

if( item.selected )

Second version, less verbose but less readable

var toggleClass = ( item.selected ? $.fn.addClass : $.fn.removeClass );$item, "Selected");

Third version, what would be nice

$item.toggleClass("Selected", item.selected);

Changed May 09, 2008 12:44PM UTC by ecentinela comment:3

An why not?

$item[item.selected ? 'removeClass' : 'addClass']("Selected");

Changed May 16, 2008 02:31AM UTC by flesler comment:4


So.. what do we do about this ?

Changed August 15, 2008 10:51AM UTC by genezys comment:5

Here are attached the modification to the core and tests to fix this bug.

Changed August 15, 2008 05:24PM UTC by flesler comment:6

cc: → dimi, ecentinela, genezys
need: Test CasePatch
owner: → flesler
status: newassigned

This is not a bug, it's an enhancement.

I think this is doable, will handle it asap.

Changed December 25, 2008 08:12PM UTC by flesler comment:7

resolution: → fixed
status: assignedclosed

Added at [6000].