4

I'm using jQuery UI .toggleClass(class, [duration]) to toggle a red background on a 100x100 box, but the results I'm getting are strange. See http://jqueryui.com/docs/toggleClass/ for reference.

As you can see from this example - http://jsfiddle.net/xkrX9/ - the first click on div#box toggles the red background immediately (without any [duration]) and then toggles it again back to white after approx 1s without a second click event. The second click (without reloading the page) results in the .red class being toggled as expected with the 1000ms duration.

What's going wrong here? Thanks for any insight!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

<style>
    #box { width: 100px; height: 100px; border: 1px solid #999; }
    .red { background: red; }
</style>

<div id="box"></div>

<script>
    $('#box').click(function() {
        $(this).toggleClass('red', 1000);
    });
</script>
klicks
  • 71
  • 1
  • 6

1 Answers1

2

It would seem that it is a bug. If there is no class attribute it will not work, but once you add the class attibute to the node it works fine,

http://jsfiddle.net/xkrX9/3/

I would file a bug report.

jQUery UI Bug #8113

Andrew
  • 13,757
  • 13
  • 66
  • 84
  • Perfect, this answers the question. Thanks for clarifying Andrew! – klicks Feb 16 '12 at 21:29
  • Thanks. I see the css `border` also acts in the similar fashion. Is it too a jQuery bug? – maan81 Mar 20 '12 at 06:38
  • @maan81 At a glance it looks like a jQuery UI bug, specifically, the animateClass function. It does a lot of `this.attr('class')` and `this.attr('style')` calls, which is the most likely culprit. – Andrew Mar 20 '12 at 14:41