Wedge

Public area => The Pub => Off-topic => Discussion démarrée par: live627 le 10 Juillet 2012 à 08:15

Titre: Set class in jQuery
Posté par: live627 le 10 Juillet 2012 à 08:15
Is there an equivalent function in jQuery that does the same thing as className other than attr()?
Titre: Re: Set class in jQuery
Posté par: Nao le 10 Juillet 2012 à 10:29
You mean .hasClass('class')...?
Or simply .is('.class')? (A bit slower though.)
If you want to assign a class, use .addClass('class') or .toggleClass('class'). The opposite is .removeClass('class').
If you want to get a list of all classes, you'll have to rely on $()[0].className or $().attr('class'), AFAIK.
Titre: Re: Set class in jQuery
Posté par: live627 le 10 Juillet 2012 à 23:40
Hmm... okay. So there's no hidden magic function that I missed. Thanks anyway, Nao!
Titre: Re: Set class in jQuery
Posté par: Nao le 11 Juillet 2012 à 00:36
Well, there's nothing wrong is using className (or anything purely JS) even when jQuery equivalents are available. I usually choose to use jQ in these situations because the code is shorter and thus compresses better.

Speaking of which, I just finished rewriting Zoomedia... The code works much better, and is about 150 bytes shorter :D
Titre: Re: Set class in jQuery
Posté par: live627 le 11 Juillet 2012 à 00:54
Same in my case... I need to set a class to multiple elements at once, and I can use JQ's .attr(), which saves me typing.