jquery-ui icon indicating copy to clipboard operation
jquery-ui copied to clipboard

switchClass() removes class if removeClass and addClass params are the same

Open gtasker10 opened this issue 1 year ago • 5 comments

I am using jQuery 3.6.0. Seems to be an undocumented "feature" with switchClass(). If I specify the same class to remove and then add, I get no class. In the following example, I would expect to see class="big":

   <input type="text" id="myInput" class="big"></input>
   <script>
      $(document).ready(function () {
         $("#myInput").switchClass("big", "big");
      });
   </script>

The above generates:

<input type="text" id="myInput" class="">

gtasker10 avatar Mar 30 '23 17:03 gtasker10

Thanks for the report. Does the issue you describe exist when jQuery UI 1.12.1 is used or only with jQuery UI 1.13.0 or newer?

mgol avatar Apr 03 '23 21:04 mgol

it's also in v1.12

https://jsfiddle.net/jgh7eu0w/

markvantilburg avatar May 21 '24 09:05 markvantilburg

https://github.com/jquery/jquery-ui/blob/9180a8180b17c38f6c3f27ba46d4546d800d3508/ui/effect.js#L232

do nothing

if(remove === add){return;}

or

		if(remove === add){
			return $.effects.animateClass.call( this, {
			add: add
		}, speed, easing, callback );
		} else {
		return $.effects.animateClass.call( this, {
			remove: remove,
			add: add
		}, speed, easing, callback );
		}

markvantilburg avatar May 21 '24 09:05 markvantilburg

Thanks for the report. Since the issue is already in 1.12, given limited team resources it's not likely to be fixed by the UI team; see the project status at https://blog.jqueryui.com/2021/10/jquery-maintainers-update-and-transition-jquery-ui-as-part-of-overall-modernization-efforts/. PRs are welcome if they're not too complex and contain tests.

mgol avatar May 21 '24 17:05 mgol

The docs don't really specify what happens if a class appears in both properties. $.effects.animateClass currently applies the options in iteration order from what I understand. And because remove is specified after add, it has precendence.

It's probably too late to change it now and I'm not even sure if that would be desired. I'd advise to just filter out the classes you don't want removing from the second argument.

mgol avatar May 21 '24 17:05 mgol