YouTip LogoYouTip

Prop Element Lastelementchild

## HTML DOM lastElementChild Property The `lastElementChild` property returns the last child element of a specified element. This property is read-only and is highly useful for navigating the Document Object Model (DOM) when you only care about HTML elements (like `
`, `
  • `, `

    `) and want to ignore whitespace, text nodes, or comments. --- ## Syntax ```javascript element.lastElementChild ``` ### Parameters * None. ### Return Value * **Type:** `Element` object (or `null`). * **Description:** Returns the last child node that is an element. If the element has no child elements, it returns `null`. --- ## Key Differences: `lastElementChild` vs. `lastChild` It is easy to confuse `lastElementChild` with the `lastChild` property. Here is how they differ: * **`lastElementChild`**: Only returns the last child **element node** (ignoring text nodes, whitespace, and comment nodes). * **`lastChild`**: Returns the absolute last child **node**, which could be an element node, a text node (including line breaks and spaces), or a comment node. ### Visual Comparison Consider this HTML structure: ```html

    • Apple
    • Banana
    ``` * `document.getElementById("myList").lastElementChild` will return the `
  • Banana
  • ` element. * `document.getElementById("myList").lastChild` will likely return a `#text` node representing the whitespace/line break after the closing `` tag. --- ## Code Examples ### Example 1: Get the HTML Content of the Last Child Element The following example retrieves the inner HTML of the last `
  • ` element inside a `
      ` list: ```html
      • Coffee
      • Tea
      • Milk
      ``` ### Example 2: Get the Tag Name of the Last Child Element This example retrieves the tag name of the last child element inside a `
      ` container: ```html

      First paragraph.

      Some inline text.
      ``` ### Example 3: Get the Text of the Last Option in a Dropdown This example retrieves the text value of the last ` ``` --- ## Technical Details | Feature | Specification | | :--- | :--- | | **DOM Version** | DOM Core Level 3 Element Traversal | | **Read-Only** | Yes | ### Browser Support The `lastElementChild` property is widely supported across all modern browsers: | Property | Chrome | Edge / IE | Firefox | Safari | Opera | | :--- | :--- | :--- | :--- | :--- | :--- | | **`lastElementChild`** | 2.0+ | 9.0+ | 3.5+ | 4.0+ | 10.0+ | --- ## Related Properties * **`firstElementChild`**: Returns the first child element of a specified element. * **`children`**: Returns a live `HTMLCollection` of all child elements of an element. * **`nextElementSibling`**: Returns the next element sibling in the DOM tree. * **`previousElementSibling`**: Returns the previous element sibling in the DOM tree.
      ← Met Element MatchesJs Class Static β†’
  • YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

    All content is for educational and learning purposes only.