Descendants Explained

by Jonny

HTML

<div id="grandpa">
    <div id="dad">
        <div id="son">Hello World</div>
    </div>
</div>

JavaScript

$( function() {
   
    // CORRECT: This will work because son is a direct descendent of dad
    var example1 = $('#dad > #son').html();
    console.log( "Example 1: " + example1);
    
    // INCORRECT: This won't work because son is not a direct descendent of grandpa
    var example2 = $('#grandpa > #son').html();
    console.log( "Example 2: " + example2);
    
    // CORRECT: Because > means direct descendent, leaving it out will get any descendent
    var example3 = $('#grandpa #son').html();
    console.log( "Example 3: " + example3);
    
});