How can I use jQuery to select all p tags that have at least one a tag as an immediate child?
For the document:
<div>
<p><a>My Link</a></p>
<p><div><a>My nested Link</a></div></p>
</div>
It would return only the first p tag.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
$(‘p:has(a)’)will do the trick.
http://jsfiddle.net/xKc8T/
Update
The issue with the
fonttag can be fixed like this:http://jsfiddle.net/xKc8T/1/
Update 2
Okay scrap all that. As per the comments the
:has()filter doesn’t just look at the direct children. So how about we go the other way and select allatags and then select the parent of those where the parent is ap.or to avoid selecting all
atags you could dohttp://jsfiddle.net/xKc8T/3/
Yet Another Update
Thanks to @BoltClock for this suggestion. The following syntax also works:
http://jsfiddle.net/xKc8T/4/