Problematic situation concerning the lack of effectiveness of jQuery's .removeClass() function

I am encountering a strange issue with some code that adds and removes classes from an element in JavaScript. Despite my best efforts, I cannot replicate the odd behavior in a simple jsfiddle example.

Below is the relevant section of JavaScript that is causing trouble:

console.log('before destroyed: ' + currentTile.get(0).className);
currentTile.addClass(classes.destroyed);
console.log('after destroyed: ' + currentTile.get(0).className);

setTimeout(function () {
    console.log('before blanking: ' + currentTile.get(0).className);
    currentTile.removeClass().addClass(classes.blank + ' ui-draggable');
    console.log('after blanking: ' + currentTile.get(0).className);
}, 2000);

The console output shows inconsistent results as follows:

While adding the destroyed class works without any issues, the problem arises when trying to remove classes using removeClass() within the setTimeout function. However, adding new classes using

.addClass(classes.blank + ' ui-draggable');
seems to work fine. It's worth noting that passing a single class to removeClass does remove that specific class successfully.

If the issue was related to context or selecting the wrong element (currentTile), I would expect the addClass method to also fail. Can anyone shed light on what might be causing this unexpected behavior?

Additional details: Using jQuery latest version (possibly v1.9.0), jQuery UI v1.10.0, Chrome browser v24.0.1312.56 m


Edit: The root cause of this issue seems to be connected to jQuery UI, and can be observed in action in this fiddle.


Edit 2: This problem has been verified as a bug in jQuery, and fortunately, it has been rectified.

Answer №1

If you want to remove a class, consider using .removeAttr('class') instead of .removeClass().

Check out this demonstration:

http://jsfiddle.net/MvvmJ/8/

I hope this information is useful. Feel free to reach out if you have any further questions!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Disappear notification with jQuery after a set amount of time

I stumbled upon this amazing script for displaying warning messages from this source: Within the script, it is configured to hide the warning message following a click event. $('.message').click(function(){ $(th ...

What is the best way to retrieve the border-color inline style using jQuery?

I have a HTML tag like this. <span id="createOrderFormId:accountNo" style="border-color: red;"><</span> To retrieve the style value for the border-color property, I tried using the following jQuery code: $( document ).ready(function() { ...

Tips for utilizing comment tags efficiently

I have encountered an issue with IE7 where my CSS is not functioning properly. In an attempt to target IE7 and lower versions specifically, I inserted IE comment tags into the HTML code. However, despite my efforts, it does not seem to be effective. Initia ...

When utilizing jQuery's .on() method, sometimes the data being passed to the handler may appear

I have three anchor tags that are dynamically added from an ajax $.get() response. Whenever one of them is clicked, I want to trigger a second request. The parameters for the second request should be set based on the global data attributes in these an ...

The function to modify text color in Yii2 using the material bootstrap library is not functioning as intended

My attempt to modify the text color or background of the footer has been unsuccessful after integrating this library. I am unable to alter even a single text color. This is my MaterialAsset: <?php /** * @link http://www.yiiframework.com/ * @copyrigh ...

Choose a HTML element <a> containing a hyperlink reference and a nested child element <li>

Does anyone know of a way in javascript/css to select the (n) <li> in my code, while also specifying that the <li> must have a parent with the current <a> tag containing a specific href link? <a href="www.link.com">CATEGORY NAME& ...

Exploring jQuery's AJAX capabilities in handling cross-domain requests and implementing Basic Authentication

I attempted the solutions provided in the suggested duplicate question, but unfortunately, they did not yield the desired outcome. I have been struggling to utilize AJAX to POST data to a remote server's API from a local PC client (testing on Chrome ...

When clicking on a video in the array, the next one will play while the index increments

It's a bit confusing... I'm struggling to articulate it properly. The issue I'm having is with a play array function I've implemented. Here's the code snippet: $("#myVid").bind("ended", function() { $("#MyT").fadeIn(25 ...

The AJAX POST request encountered an error

I apologize for the lackluster title. Basically, when the script is executed, it triggers an 'error' alert as shown in the jQuery code below. I suspect that the issue lies in the structure of my JSON data, but I'm uncertain about the necess ...

Click on the jq toggle button to increase productivity and complete additional tasks

Is it possible to use a toggle function to create a drop-down menu while also rotating the mobile menu icon image when clicked? Can this be accomplished within the same function, or would I need to add a new event listener to the image? jQuery('#mobi ...

Solution for Organizing Tables

I've sourced data from various JSON API links and displayed it in a table. Currently, my code looks like this: <script src="js/1.js"></script> <script src="js/2.js"></script> Above this code is the table structure with <t ...

Is it possible to achieve the same functionality as :first-child by employing :nth-of-type without a selector?

Currently tackling a console warning related to MUI5/emotion (I believe) I have noticed that the pseudo class ":first-child" might pose potential risks during server-side rendering. It may be worth considering replacing it with ":first-of-type". I wonder ...

Tips for setting a value from an AJAX-created element into a concealed form field

There is a div that contains a button. When the button is clicked, a form appears. The div's id is then assigned to a hidden field within the form. This functionality works for the divs that are present on the page when it initially loads, but it does ...

Enable arrow keys feature in Regular Expressions

Currently, I am implementing alphanumeric validation to ensure that users can only input alphanumeric values and also paste alphanumeric values exclusively. In order to achieve this, I have utilized the following regular expression: function OnlyAlphaNum ...

AdminLTE-3 Bootstrap-4 sidebar menu with dynamic AJAX functionality fails to toggle treeview open/hide after page is fully loaded

I am looking to update the sidebar menu dynamically in the adminlte 3 dashboard with bootstrap 4 using AJAX calls. However, I have run into an issue where the menu open/close functionality is not working when the data is loaded dynamically using AJAX. On t ...

Dynamic loading of XML data into a DIV when hovering over it

The main idea behind my project is quite simple. I have a grid of company logos extracted from an XML document using XSLT, each logo with its own unique link to the respective company profile. On the same page, I have a separate div that serves as a "prev ...

Adjust scrollbar thumb size using CSS

I am looking to customize my scrollbar with a larger thumb size compared to the track. While I can adjust the color and other properties easily, I am struggling to change the sizes of the thumb and track separately. .custom_scrollbar::-webkit-scrollbar { ...

What steps are involved in incorporating dynamic pagination in PHP using this particular snippet of code?

I am looking for a way to display a list of numbers from 1 to 1000 in a single line with the ability to scroll. However, I need to exclude both "Prev" and "1" from this list. How can I achieve this? Below is the code snippet that I currently have: echo ...

Table pagination in Mat is experiencing overflow, and the button styles have also been customized for a unique look

I implemented a mat paginator feature, however, I am facing an issue where the alignment of items per page options is not correct. Below is the snippet from component.html file: <div class="table-responsive" *ngIf="resultssummaries"> &l ...

Switching Background Colors with CSS

Can you change background colors using only CSS3? I attempted to use keyframe animations, but they only transition the background color. I simply want the background color to change after 5 seconds with no transition or hover effects. If I can fade it in ...