tags:

views:

32

answers:

2

I have the following jQuery calls chain:

$(someSelector).nextUntil(".specialClass").addClass(classBasedSomeSelectorObject)

I need to addClass that's value is based on the object which was returned by someSelector.

Is there any way to implement it?

+2  A: 

Wrapping this in a function seems to be the simplest approach, something like this:

function(selector) {
  var class = selector == ".thing" ? "thingClass" : "defaultClass";
  $(selector).nextUntil('.specialClass').addClass(class);
}

Update based on comments:

$(selector).each(function() {
  var class = "class" + this.id; //whatever logic to get class here
  $(this).nextUntil('.specialClass').addClass(class);
});
Nick Craver
You misunderstood me. My classBasedSomeSelectorObject should be based on the object's returned by selector id
Idsa
@Idsa - Added an example for that just now, if you can say exactly what about the object it depends on I can fine tune it. If you want to apply it to **only** the elements from the selector, and not including the `.nextUntil()`, just move `.nextUntil()` after the each in the chain, so instead of `.nextUntil().each()`, do `.each().nextUntil()`
Nick Craver
@Nick Craver: I need to generate class using selector result object id, but to add this class to the object returned by nextUntil. You are operating with $(this) in both cases...
Idsa
@Idsa - I think I understand you now, updated again...foe *each* match in the first selector, it uses that match's id and applies that class to **it's** set of `nextUntil()` elements, so each one and it's nextUntil elements are operating with the same class, if that's still not what you mean can you clarify further?
Nick Craver
I think our answers have converged :)
araqnid
@araqnid: you know what they say, in the end there can be only one. :P
R0MANARMY
@araqnid - You, you posted shortly after my edit arriving at the same conclusion :)
Nick Craver
Thank you, guys! I mark araqnid's post as answer, because he was a bit quicker :)
Idsa
@Idsa - This was edited/updated 19 minutes ago, his was posted 18 minutes ago, so that's incorrect, but glad you got it working :)
Nick Craver
in my defence, I think I was writing while Nick was editing and SO only tells you about simultaneous answers, not edits :p
araqnid
+1  A: 
$('someSelector').each(function() {
    $(this).nextUntil('.specialClass').addClass(/* calculation based on 'this' */);
});
araqnid