jQuery Selector How to - Select only images that wrapped in links








Question

We would like to know how to select only images that wrapped in links.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from ww w  .  j a  v  a2  s . c o m-->
    $result = $("a > #MyImage");
    console.log($result.length);
});
</script>
</head>
<body>
  <a href='' ><img id="MyImage"></a>

</body>
</html>

The code above is rendered as follows: