jQuery Selectors: Selects element that contains a specific child element $(‘has(E)’)

This entry is part 15 of 33 in the series jQuery Selectors

$(‘has(Element)’)

In the example below, it will select all <div> elements that contain a <span> element.

<!DOCTYPE html>
<html>
<head>
    <script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
    <script>
        $(function () {
            $('div:has(span)').css('color', 'blue');
        });
    </script>
</head>
<body>
<div>
    <h4>Ushuaia</h4>

    <p>Southernmost city in the world.</p>
</div>
<div>
    <p>The city centered around a prison built by the Argentine government.</p>

    <p><span>They also built a railway to the settlement, now a tourist attraction known as the End of the World Train</span></p>

    <a href='http://en.wikipedia.org/wiki/Ushuaia'>Visit us here.</a>
</div>
</body>
</html>

Series Navigation<< jQuery Selectors: Content filter selector-contains
jQuery Selectors: Selects elements that have no content >>