instance method Element#firstDescendant

View source on GitHub →

Element#firstDescendant() → Element

Returns the first child that is an element.

This is opposed to the firstChild DOM property, which will return any node, including text nodes and comment nodes.

<div id="australopithecus">
  <div id="homo-erectus"><!-- Latin is super -->
    <div id="homo-neanderthalensis"></div>
    <div id="homo-sapiens"></div>


// -> div#homo-erectus
 // the DOM property returns any first node
// -> comment node "Latin is super"
 // this is what we want!
// -> div#homo-neanderthalensis

This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.