Ticket #5873 (closed bug: fixed)
removeClass leaves space after removing last class
| Reported by: | dmifedorenko | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.4 |
| Component: | core | Version: | 1.4 |
| Keywords: | removeClass space | Cc: | |
| Blocking: | Blocked by: |
Description
Windows XP, Firefox 3.5.7.
Then '.removeClass("disabled")' called for '<tr class="disabled">' the result will be '<tr class=" ">'.
But expected result is '<tr class="">'.
Test case:
<html> <body> <script type="text/javascript" src=" http://code.jquery.com/jquery-1.4.js"></script> Why removeClass add space if here is no class?!
<table> <tr class="disabled">
<td><h1 onclick="var tr = $(this).parents('tr'); tr.removeClass('disabled'); $('body').append('table row class after removeClass = \+tr.attr('class')+'\'<br/>')">click me</h1></td>
</tr> </table>
<pre> To fix: in "removeClass" method after elem.className = className.substring(1, className.length - 1); add if (elem.className == " ") elem.className = ""; </pre> <br/> </body> </html>
Change History
comment:2 Changed 3 years ago by addyosmani
The length of the class once you've removed it does in fact seem to be 1 (ie the space mentioned in the original ticket).
Looking at core, this seems to be being done on purpose in the removeClass function around lines 1297-1282:
var className = (" " + elem.className + " ").replace(rclass, " "); for ( var c = 0, cl = classNames.length; c < cl; c++ ) { className = className.replace(" " + classNames[c] + " ", " "); }
Whilst I think a second review to confirm this behavior's need would be useful, I also question what (if any) impact it being in place has on a developer's ability to modify the class at a later stage as it is just afterall a space.
Live test case: http://jsbin.com/obeji4/2/edit
Flagging for second review.
comment:4 Changed 3 years ago by paulelliott
This bug appears to be a duplicate of #5988. It was tested and fixed in the 1.4.2 release.
http://github.com/jquery/jquery/commit/e76ba32cebc26108e15b2e0e03c5152624c1bad9
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Hey?! Any body?! Can you fix it? :)