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

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


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

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

    <p>Southernmost city in the world.</p>
    <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>

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

Leave a comment

Leave a Reply