Jörn Zaefferer wrote:
Sam Collett schrieb:
$('.a')[0].className should work
That doesn't explain why .attr('class') should not work. I've given that a quick try and I have no problems, it returns both classes.

Looks like my test case wasn't specific enough. When more than one object matches, jQ returns the first class.

<div class="a b"></div>
<div class="a c"></div>

$(this).attr("class")

returns: "a b"


I think I need to change my approach, anyway. I was chaining things together expecting sequential end-to-end processing of single objects, which is incorrect. Each link in the chain is fully processed for all objects, and then the next link is processed.

I'm was trying to remove the class from on object and put it on a newly created object in the chain.

$(document).ready(function(){
   $(".a")
      .wrap('<div class="outter"></div>')
      .removeClass()   // from the current object
      .addClass("z")   // give current object a new class
      .parents(".outter")   // back up to the new div
.addClass(?xyz?); // give the new div the class of the original object
});

So how do I reference the class of the currently processed object? I suspect I cannot, given the processing order. If I could get the current object class I could do this:

$(document).ready(function(){
   $(".a")
      .wrap('<div class="outter' +original_class+ '"></div>')
      .removeClass()   // from the current object
      .addClass("z");   // give current object a new class
original object
});

Is there a way for some code to get the class of the currently processing object at original_class?

Am I just going the wrong way? Should I switch to normal loop, or an each loop? Comments?

 ~ ~ Dave

Test code: http://solidgone.com/jquery/classes_test.html

Reply via email to