- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM firstChild Property
The HTML DOM firstChild property is used to access and retrieve the first child node of a specified parent element. In the HTML DOM, an element cannot be considered a parent or child node unless it is compared to other nodes (elements).
For example, an individual <ul> element is not inherently a parent or child element. However, when compared to <li> elements, the "ul" is considered the parent, while the "li" elements are considered its children.
Syntax
Following is the syntax of the HTML DOM firstChild property −
element.firstChild;
Parameters
Since it is a property, it does not accept any parameters.
Return Value
This property returns the first child of a given parent element. If no first child exists it returns 'null'.
Example 1: Accessing the First Child Node of a Div Element
The following program demonstrates how to use the HTML DOM firstChild property to access the first child node of a <div> element −
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM firstChild</title>
</head>
<body>
<h4>Displays the first child Node...</h4>
<div id="parent">
<p>This is the first child node.</p>
<p>This is the second child node.</p>
</div>
<div id="otput"></div>
<script>
var parent = document.getElementById("parent");
var firstChild = parent.firstElementChild;
function displayResult() {
var outputDiv=document.getElementById("otput");
outputDiv.textContent = "First Child Node: " + firstChild.textContent.trim();
}
// Call the function to display the result
displayResult();
</script>
</body>
</html>
The above program accesses and returns the first child node of the "div" (i.e., parent node) element.
Example 2: Handling no child Nodes
Following is another example of the HTML DOM firstChild property. We use this property to check whether the element has any child node or not −
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM firstChild</title>
</head>
<body>
<h4>This example does not have any child nodes...</h4>
<div id="pt"></div>
<div id="output"></div>
<script>
var parent = document.getElementById("pt");
var firstChild = parent.firstChild;
var resultMessage;
if (firstChild === null) {
resultMessage="The element has no child nodes.";
} else {
resultMessage = firstChild.textContent.trim();
}
// Function to display result in output div
function displayResult() {
var odiv = document.getElementById("output");
odiv.textContent = resultMessage;
}
displayResult();
</script>
</body>
</html>
Example 3: Displays the first Child Node of Body Element
This example shows the use of the firstChild Property. It retrieves the first child node of the body element, which is a <p> element −
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM firstChild</title>
</head>
<body>
<p>Displays first child node of body</p>
<h4>Welcome to Tutorialspoint</h4>
<div id="ot"></div>
<script>
// Selecting the body element
var bodyElement = document.body;
// Retrieving and displays the first child node
var firstChild = bodyElement.firstChild;
function displayResult() {
var otdiv = document.getElementById("ot");
otdiv.innerHTML = "First Child Node of Body:" + firstChild.nodeName;
}
displayResult();
</script>
</body>
</html>
Example 4: Get the first child of ul Element
This example shows how to access the first child node of a<ul>element with the ID "myList" using the firstChild property −
<!DOCTYPE html>
<html lang="en">
<head>
<title>Retrieving First Child Node HTML</title>
</head>
<body>
<p>Displays HTML content of first child node of "ul" element.</p>
<ul id="myList">
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
</ul>
<button onclick="getFirstChild()">Get First Child Node</button>
<div id="ot"></div>
<script>
function getFirstChild() {
// Accessing the ul element
const ul = document.getElementById('myList');
const firstChildNode = ul.firstChild;
if(firstChildNode.nodeType === Node.TEXT_NODE){
const firstChildElement = firstChildNode.nextSibling;
const otDiv = document.getElementById('ot');
otDiv.innerHTML = `<p>HTML content of first child node: </p>${firstChildElement.outerHTML}`;
}
else{
const otDiv = document.getElementById('ot');
otDiv.innerHTML = 'No child node found..!';
}
}
</script>
</body>
</html>
Supported Browsers
| Property | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| firstChild | Yes | Yes | Yes | Yes | Yes |




