Posted By

quickredfox on 09/04/09


Tagged

class jquery repalce


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bdelcamp


jQuery.replaceClass();


 / Published in: jQuery
 

I notice a lot of addClass/removeClass.. thought maybe I'd post an example on how to simplify that....

  1. /*
  2. ** jquery.replaceClass.js -- jQuery "plugin", helps classname manip.
  3. ** Copyright (c) 2007 Francois Lafortune (quickredfox.at)
  4. ** Licensed under MIT
  5. **
  6. */
  7. jQuery.fn.replaceClass = function(toReplace,replaceWith){
  8. return $(this).each(function(){
  9. return $(this).removeClass(toReplace).addClass(replaceWith);
  10. });
  11. }
  12.  
  13. // Of course, you'd rarely ever save this file like this, what you wanna do
  14. // is create a little extensions file for all your tiny little jQuery
  15. // quirks and re-use that...
  16. jQuery.extend(jQuery.fn,{
  17. replaceClass = function(){//code...},
  18. otherHelper = function(){ //... }
  19. // etc ...
  20. })

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: joe-bbb on September 28, 2009

Thats nice, however shouldn't that be a colon not an equals? e.g.

//jQuery extension object jQuery.extend(jQuery.fn,{ replaceClass : function(toReplace,replaceWith){ return $(this).each(function(){ return $(this).removeClass(toReplace).addClass(replaceWith); }); } })

//call $('#first-col').click(function(){ $(this).replaceClass('person','dog'); });

Posted By: jordansair002 on October 7, 2010

The Air Jordan shoes Michael Jackson Edition is inspired by the Cheap Jordans. There are multiple designs, which can be viewed our Nike jordan website. This has for being the a single issue the ultimate MJ admirer is lacking in research of the total outfit, unless they program on obtaining Air Yeezy to launch some specific edition socks Air Yeezy shoes too.

You need to login to post a comment.