views:

34

answers:

4

I have an element in the DOM d in a jQuery object and I'd like to count the number of elements matching font[color=#ff6600] before the element. Is there a CSS selector I can use for this?

A: 

.nextAll() maybe?

http://api.jquery.com/nextAll/

Nikita Prokopov
Nah, I'd like to include things other than direct siblings, just as long as they appear later in the DOM.
Andrey Fedorov
(that is, siblings+their descendants, of the element, and also of its ancestors)
Andrey Fedorov
A: 

I would look at the selectors .siblings() and .parents() along with .context to meet this specific requirement.

Floyd Pink
A: 

Your question is imprecise so this is my best guess

$(d).prevAll('font[color=#ff6600]').length

If you mean to collect all font[color=] elements that appear lexically before d then you can't do this with a pure selector. you'll need to iterate over a larger matching set

Scott Evernden
What can I clarify to make it precise?
Andrey Fedorov
I think this is very close, but I need to search the siblings' descendants, as well as the siblings and descendants of all ancestors of `d`.
Andrey Fedorov
+1  A: 

I think this is what I was looking for. Will test thoroughly in a bit:

$(d).parents().andSelf().prevAll().find('font[color=#ff6600]')
Andrey Fedorov