Wouldn't your code be faster if you did it without generating a new jQuery Object for each element you're doing this for. Also, you could just use the internal jQuery.className.has(el,className) rather than .is('.'+className) since .is() still needs to regex parse your input string to determine that you're looking for a className followed by the actual className test whereas jQuery.className.has(el,className) just needs to do the className test.

$.fn.swapClass = function(c1, c2){
    return this.each(function(){
        if (jQuery.className.has(this,c1)) {
            jQuery.className.remove(this,c1);
            jQuery.className.add(this,c2);
        } else {
            jQuery.className.remove(this,c2);
            jQuery.className.add(this,c1);
        }
    });
}
It's a marginal improvement but one that would be felt on large projects that use this frequently.

-blair

Wil Stuckey wrote:
Only if the selected elements have either c1 or c2 initially. If they have
neither or both, it won't yield the same results.

Right this was my intention. I changed the plugin a bit to make it more readable, IMO. What it does is looks for c1 if it's not present it adds it. Then next time swapClass is run it finds c1 so it is removed and c2 is added.

You can see that here:
http://sandbox.wilstuckey.com/jquery/swapClass.html

-Wil


_______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/



_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to