[jQuery] Beginner question re: getting and setting element class name
Hi, I'm just starting to use jQuery and, while I'm not much of a coder,
I love the way it works. I was developing a project using the Prototype
library, but after discovering jQuery I'm converting everything. I'm
finding it straightforward but I have a request for a clarification on
something.
I have a function that branches depending on the class name of the element that was passed to it.
Using Prototype:
function checkState(id) {
if ($(id).className == "something") { etc
but in jQuery it doesn't work. It does work if I use:
if ($(id).attr("class") == ""something") { etc
I'm just wondering, as a beginner, if that would be the most 'correct' way to do it.
Also, if I want to toggle the class name of an element between say "active" and "inactive" what is the easiest way to do that? From the documentation I see that addClass won't replace the existing class so it looks like the best way is to use the attr(key,value) method and do something like:
$(id).attr("class", "active")
It works, I'm just looking for 'best practise' advice.
Thanks
<span class="sg">Miles</span>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/