Bug Tracker

Modify

Ticket #1681 (closed enhancement: fixed)

Opened 6 years ago

Last modified 4 years ago

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
Blocking: Blocked by:

Description

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

src-1681.diff Download (600 bytes) - added by genezys 5 years ago.
test-1681.diff Download (810 bytes) - added by genezys 5 years ago.

Change History

comment:1 Changed 6 years ago by brandon

  • need changed from Review to Test Case

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

comment:2 Changed 5 years ago by genezys

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 )
{
  $item.addClass("Selected");
}
else
{
  $item.removeClass("Selected");
}

Second version, less verbose but less readable

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

Third version, what would be nice

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

comment:3 Changed 5 years ago by ecentinela

An why not?

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

comment:4 Changed 5 years ago by flesler

  • Milestone changed from 1.2.2 to 1.2.4

So.. what do we do about this ?

Changed 5 years ago by genezys

Changed 5 years ago by genezys

comment:5 Changed 5 years ago by genezys

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

comment:6 Changed 5 years ago by flesler

  • Cc dimi, ecentinela, genezys added
  • need changed from Test Case to Patch
  • Status changed from new to assigned
  • Owner set to flesler
  • Milestone changed from 1.2.4 to 1.3

This is not a bug, it's an enhancement. I think this is doable, will handle it asap.

comment:7 Changed 4 years ago by flesler

  • Status changed from assigned to closed
  • Resolution set to fixed

Added at [6000].

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.