jQuery to select where no children or first child is not image

Posted by hacker on Stack Overflow See other posts from Stack Overflow or by hacker
Published on 2010-05-20T15:15:41Z Indexed on 2010/05/20 15:40 UTC
Read the original article Hit count: 741

Filed under:
|
|

I'm trying to dynamically style some elements on my pages. So, if I had the following code:

<a href="#">
  Select Me
</a>

<a href="#">
  <img src="blah.jpg" />
  Don't select me
</a>

<a href="#">
 <div>Don't select me either</div>
 <img src="blah.jpg" />
</a>

<a href="#">
  <div>You can select me too.</div>
</a>

I would like it to select the first and fourth tags.

From what I could tell, by using:

 $("a:first-child")

Won't select the first tag because it doesn't have any children (just text). The second tag should not get selected so something like:

$("a:first-child").not("img)

but that leaves out the first item.

EDIT: If there is an IMG anywhere in the A element, don't select it.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors