Having .hasClass / .removeClass support regex has been discussed before,
there is a ticket open for one of them so it might be a possibility.
I don't see much use for replaceClass, rather I think the semantics of
reading it would be a little confusing. I can't even understand what the
code you have there is even supposed to do.
I see no reason to combine add and remove into one.
Just a quick tip, if you're trying to do boolean stuff:
$(node).filter('.color-black').removeClass('color-black').addClass('color-white').end()...
That there would replace color-black with color-white but only for nodes
with color-black, the .end() returns back to the original $(node) so
chaining can continue.
~Daniel Friesen (Dantman, Nadir-Seen-Fire)
Julian Aubourg wrote:
> Hey all,
> I'm working on a site that offers some customizability regarding elements
> colors (background & font). Rather then setting the color with .css(), I use
> classes like color-red, color-green, etc (and of course, red is not the css
> "red" and green is not the css "green"). I find it much cleaner since I
> need not have color values referenced within javascript or php (it's simply
> in the css).
>
> However, the code seems unnecessarily bloated for switching classes. What
> would be very useful imo, is to have hasClass accept regexp & return the
> array of found classes and also a .replaceClass() function that would change
> a class into another. That way I could do the following:
>
> var colorClasses = $.hasClass(/color-.+/);
> if (colorClasses!==false) {
> var currentColorClass = colorClasses[0];
> }
> // Do stuff with the color class
> $.replaceClass(currentColorClass,"color-"+newColor);
>
> As of today, I have only two solutions:
> - keep the current color using $.data() or any other custom data container
> - OR search the class attribute by hand (hoping classes are in a specific
> order, you can imagine the mess)
>
> If that wasn't bad enough, after that, to replace the class, I have to call
> $.removeClass() and $.addClass() which seems pretty inefficient to me.
>
> I know I could probably do a plugin for that but I have the feeling that, if
> embedded in jQuery with its class related code, it would be much more
> efficient.
>
> I also know I could switch back to the .css("background-color",color) but it
> kinda defeats the purpose of having the presentation information in the css
> and ONLY in the css which I find pretty nice for maintenance.
>
> Yes, and finally, am I crazy or couldn't this be quite useful?
>
> Take care, all,
>
> -- Julian
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---