[jQuery] Re: jquery toggle class, I needed to switch class....

2009-04-12 Thread dongle

didnt you forget jQuery.fn.switchClass=function ???

On Mar 23, 7:47 pm, Eric Garside gars...@gmail.com wrote:
 Or:

 jQuery.fn.switchClass( a, b ){
    var t = this.hasClass(a);
    this.addClass( t ? b : a ).removeClass( t ? a : b );

 }

 On Mar 23, 12:35 pm, T.J. Crowder t...@crowdersoftware.com wrote:

  Hi,

  You're creating (or worse, overwriting) global variables 'remove' and
  'add' there (because you haven't given the 'var' keyword, and thus are
  creating implicit globals[1]).  The temporaries don't really buy you
  anything anyway, perhaps simply:

  jQuery.fn.switchClass = function(class1,class2) {
      if (this.hasClass(class1)) {
          this.removeClass(class1).addClass(class2);
      } else {
          this.removeClass(class2).addClass(class1);
      }

  };

  [1]http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html

  FWIW,
  --
  T.J. Crowder
  tj / crowder software / com
  Independent Software Engineer, consulting services available

  On Mar 23, 1:33 pm, Artistan artis...@gmail.com wrote:

   ///
   ///   Switch Between Classes    ///
   ///
   jQuery.fn.switchClass = function(class1,class2) {
       if(this.hasClass(class1)){
           remove = class1;
           add = class2;
       } else {
           remove = class2;
           add = class1;
       }
       this.removeClass(remove);
       this.addClass(add);

   };

   Hope this helps someone out.


[jQuery] Re: jquery toggle class, I needed to switch class....

2009-04-12 Thread Ricardo

This is exactly the same as

$(selector).toggleClass(a).toggleClass(b)

On Mar 23, 2:47 pm, Eric Garside gars...@gmail.com wrote:
 Or:

 jQuery.fn.switchClass( a, b ){
    var t = this.hasClass(a);
    this.addClass( t ? b : a ).removeClass( t ? a : b );

 }

 On Mar 23, 12:35 pm, T.J. Crowder t...@crowdersoftware.com wrote:

  Hi,

  You're creating (or worse, overwriting) global variables 'remove' and
  'add' there (because you haven't given the 'var' keyword, and thus are
  creating implicit globals[1]).  The temporaries don't really buy you
  anything anyway, perhaps simply:

  jQuery.fn.switchClass = function(class1,class2) {
      if (this.hasClass(class1)) {
          this.removeClass(class1).addClass(class2);
      } else {
          this.removeClass(class2).addClass(class1);
      }

  };

  [1]http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html

  FWIW,
  --
  T.J. Crowder
  tj / crowder software / com
  Independent Software Engineer, consulting services available

  On Mar 23, 1:33 pm, Artistan artis...@gmail.com wrote:

   ///
   ///   Switch Between Classes    ///
   ///
   jQuery.fn.switchClass = function(class1,class2) {
       if(this.hasClass(class1)){
           remove = class1;
           add = class2;
       } else {
           remove = class2;
           add = class1;
       }
       this.removeClass(remove);
       this.addClass(add);

   };

   Hope this helps someone out.


[jQuery] Re: jquery toggle class, I needed to switch class....

2009-03-23 Thread T.J. Crowder

Hi,

You're creating (or worse, overwriting) global variables 'remove' and
'add' there (because you haven't given the 'var' keyword, and thus are
creating implicit globals[1]).  The temporaries don't really buy you
anything anyway, perhaps simply:

jQuery.fn.switchClass = function(class1,class2) {
if (this.hasClass(class1)) {
this.removeClass(class1).addClass(class2);
} else {
this.removeClass(class2).addClass(class1);
}
};

[1] http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html

FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Mar 23, 1:33 pm, Artistan artis...@gmail.com wrote:
 ///
 ///   Switch Between Classes    ///
 ///
 jQuery.fn.switchClass = function(class1,class2) {
     if(this.hasClass(class1)){
         remove = class1;
         add = class2;
     } else {
         remove = class2;
         add = class1;
     }
     this.removeClass(remove);
     this.addClass(add);

 };

 Hope this helps someone out.


[jQuery] Re: jquery toggle class, I needed to switch class....

2009-03-23 Thread Eric Garside

Or:

jQuery.fn.switchClass( a, b ){
   var t = this.hasClass(a);
   this.addClass( t ? b : a ).removeClass( t ? a : b );
}

On Mar 23, 12:35 pm, T.J. Crowder t...@crowdersoftware.com wrote:
 Hi,

 You're creating (or worse, overwriting) global variables 'remove' and
 'add' there (because you haven't given the 'var' keyword, and thus are
 creating implicit globals[1]).  The temporaries don't really buy you
 anything anyway, perhaps simply:

 jQuery.fn.switchClass = function(class1,class2) {
     if (this.hasClass(class1)) {
         this.removeClass(class1).addClass(class2);
     } else {
         this.removeClass(class2).addClass(class1);
     }

 };

 [1]http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html

 FWIW,
 --
 T.J. Crowder
 tj / crowder software / com
 Independent Software Engineer, consulting services available

 On Mar 23, 1:33 pm, Artistan artis...@gmail.com wrote:

  ///
  ///   Switch Between Classes    ///
  ///
  jQuery.fn.switchClass = function(class1,class2) {
      if(this.hasClass(class1)){
          remove = class1;
          add = class2;
      } else {
          remove = class2;
          add = class1;
      }
      this.removeClass(remove);
      this.addClass(add);

  };

  Hope this helps someone out.