Reputation: 1826
I guess this kind of question is easy for half of you out there, but I'm stuck on this for hours, so, I'll very much appreciate your help. Here is what I have been struggling with:
I need to select all link element that contain an href
attribute which ends with .pdf
. I came with this solution:
$('a[href$=".pdf"]').filter(function() {
return $(this).parent().has('ul');
})
})
However, It seems that $(this).parent().has('**anytag**')
always returns true regardless of what "anytag"
is (I've checked this with firebug).
So, my code is broken but I cannot understand why it has this unexpected behavior. Could you point me out what is my misunderstanding here?
Upvotes: 1
Views: 1073
Reputation: 154888
.has()
is a filter function itself. It returns a new jQuery set with elements that have elements matching the selector as their descendants. Since jQuery sets are thruthy, returning them in .filter()
has no effect, as nothing gets filtered out.
You could instead use the :has
selector combined with the child selector:
$(':has(ul) > a[href$=".pdf"]');
Upvotes: 3