
- 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 Element firstElementChild Property
The HTML DOM Element firstElementChild property is used to access and retrieve the first child element within a given parent element. The child element refers to those elements that are present within a particular element.
It will not access any text or comment nodes, these will be ignored. If no child element is found within the parent element, it will return null.
Syntax
Following is the syntax of the HTML DOM Element firstElementChild property −
element.firstElementChild;
Parameters
Since this is a property, it will not accept any parameter.
Return Value
This property returns the first child element of a particular parent element or returns 'null' if no child elements are present.
Example 1: Accessing the First List Item
The following program demonstrates the usage of the HTML DOM Element firstElementChild property by accessing the first item of the given list (<ul>) −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element firstElementChild</title> </head> <body> <h3>HTML DOM Element firstElementChild Property</h3> <p>Access and Displays the first child Element..</p> <ul id="myList"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <p id="output"></p> <script> // Accessing the ul element const ulEle=document.getElementById('myList'); //Retrieves the first list item const firstListItem = ulEle.firstElementChild; // Displaying the text content of the first item const outDiv=document.getElementById('output'); outDiv.textContent = `First list item: ${firstListItem.textContent}`; </script> </body> </html>
The above program retrieves the first child element of a list.
Example 2: Accessing the First Child Element of a Div
Following is another example of the HTML DOM Element firstElementChild property. We use this method to retrieve the first child of the <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element firstElementChild</title> </head> <body> <h1>HTML - DOM Element</h1> <h2>firstElementChild Property</h2> <p>Click the button to retrieve the first child of a "div" element.</p> <div id="parent"> <span>Span Element</span> <div>Second Div</div> <p>Last Paragraph</p> </div> <hr> <button onclick="retrieveFirstChild()">Retrieve First Child Element</button> <p id="output"></p> <script> function retrieveFirstChild() { const pDiv=document.getElementById('parent'); const fcele = pDiv.firstElementChild; // Displaying the tag name of the first child const oD = document.getElementById('output'); oD.textContent=`First child element tag name: ${fcele.tagName}`; } </script> </body> </html>
When the button is clicked, the first child element of the 'div' element will be displayed.
Example 3: Handling Absence of Child Elements
In the example below, we use the firstElementChild property to retrieve the first child element of a form. Since no child elements are present, it will return null −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element firstElementChild</title> </head> <body> <h3>HTML DOM Element firstElementChild Property</h3> <p>Click the button to retrieve the first Input field</p> <form id="myForm"> </form> <p id="output"></p> <button onclick="getFirstInput()">Get First Input</button> <script> function getFirstInput() { var form = document.getElementById("myForm"); var finp = form.firstElementChild; // Displays placeholder of the first input var outdiv = document.getElementById('output'); outdiv.innerHTML = `<p>First Input Field: ${finp}</p>`; } </script> </body> </html>
When the button is clicked, it will display null because there are no child elements within the form.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
firstElementChild | Yes | Yes | Yes | Yes | Yes |