No, you can't check for parent conditions with is(). You can test descendants
to a certain extent, but not parents.
The same goes for filter() - unless you use filter(function), in which case
you do more or less what you like!
Also, using hierarchical selectors as filters won't work - they're
selectors, not filters.

The third of your filter() tests ...
     alert ( $('div').filter('div:not(.test) div').size() ); // Alerts 3
... is treating the filter as 2 separate filters and adding the results, eg
filter('div:not(.test)') + filter('div').
The documentation says that this should be achieved by comma-delimiting the
2 filters - ie. filter('div:not(.test),div') - not space-delimiting, so
maybe it's a bug, maybe not. Either way, it won't do want you want it to do.

Is there a better way than...?
if (o.parents().filter('div.unwantedclass').size()==0) { ... }

It really depends on what you're using the 'o' collection for. The obvious
alternative is not to put into 'o' anything that has a parent of class
'unwanted' to start with, eg o = $('div:not(.unwantedclass)>*');

If you can't do that, then there's nothing wrong with what you have, but a
filter alternative (>=v1.2) could be...

o.filter(function(){return !$(this).parent().is('unwantedclass');});

...which would remove any elements whose parent has a class of
'unwantedclass'.


Matt Kruse-2 wrote:
> 
> 
> It seems that I have a misunderstanding about how .is() worked. Given
> this html:
> <div class="test">
>       <div id="x">Test DIV</div>
> </div>
> 
> I was expecting this:
>      alert( $('#x').is('div:not(.test) div') );
> to return "false". Instead, it returns true.
> 
> Can I not check for parent conditions in .is()?
> 
> I see that according to the docs, is() uses filter() internally. So I
> tried this:
>      alert ( $('div').size() ); // Correctly alerts 2
> and this:
>      alert ( $('div:not(.test) div').size() ); // Correctly alerts 0
> but this:
>      alert ( $('div').filter('div:not(.test) div').size() ); // Alerts
> 3
> 
> It looks like the internal div is being duplicated in the results,
> which I don't understand either. I would have expected 0, because no
> div elements on the page actually match the filter() expression. I am
> probably just misunderstanding the internal workings of these methods
> - can anyone clarify?
> (all my testing is in IE6 only, btw)
> 
> My real-world goal is to make sure an element has no parent with a
> certain class, and take action only if this is true. I wanted to use
> the .is() method above, but I am doing this instead for now:
> 
> if (o.parents().filter('div.unwantedclass').size()==0) { ... }
> 
> Is there a better way?
> 
> Matt Kruse
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/.is%28%29-cannot-check-against-the-parent-chain--tf4556009s27240.html#a13010056
Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to