Posted By


quickredfox on 09/04/09

Tagged


Statistics


Viewed 1109 times
Favorited by 0 user(s)

jQuery.replaceClass();


/ Published in: jQuery
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.