addClass() passing function as parameter doesn't behave as expected

addClass() passing function as parameter doesn't behave as expected

  1. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

  2. <p></p>
  3. <p></p>

  4. <script>
  5. $target=$('p').first();
  6. $target.text('plz');
  7. $target.addClass('red');
  8. $target.addClass(
  9. function(i,c)
  10.   {
  11.   var added;
  12.   if (c==='red')
  13.     {
  14.     this.removeClass('red');
  15.       added='blue';
  16.     }
  17.     return added;
  18.   }
  19. );
  20. </script>

  21. <style>
  22. .blue 
  23. {
  24.   color: blue;
  25. }

  26. .red
  27. {
  28.   color: red;
  29. }
  30. </style>

The text doesn't turn blue.