firstChild (W3C DOM Core property)

Spec
Version Depr. Static Read-only
DOM1 No No Yes
Browser support (more…)
IE5.5+ FF1.5+ SA1.3+ OP9+
Full Full Full Full

Example

var child = node.firstChild;

In the example above, if node has any immediate child nodes (of any type), the child variable will be a reference to the first one; otherwise it will be null.

In the following HTML example, the firstChild of the p element is a dfn element, and the firstChild of the dfn element is a Text node with the value W-30 Sampling Keyboard:

<p>
  <dfn>W-30 Sampling Keyboard</dfn> was made by Roland in 1989,
  and popularized by <cite>The Prodigy</cite>, who used it extensively
  on their landmark debut album, <cite>Experience</cite>
</p>
Note: This example doesn’t take account of whitespace

Actually this example is idealized, and in some browsers the dfn element may not in fact be the firstChild of the p element; that's because some browsers count intermediate whitespace as text nodes, and would therefore consider the firstChild to be the whitespace between the p and dfn tags.

For more about this behavior please see DOM Core.

Description

The first node that is a direct child of this node (ie. this node is its parent and it appears at the beginning of the childNodes collection), or null if there is no such node.

This property is readonly.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Full Full Full Full Full Full Full Full Full Full Full

No known issues.

User-contributed notes

There are no comments yet.

Related Products