/Web APIs

DocumentFragment: getElementById() method

The getElementById() method of the DocumentFragment returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

If you need to get access to an element which doesn't have an ID, you can use querySelector() to find the element using any selector.

Note: IDs should be unique inside a document fragment. If two or more elements in a document fragment have the same ID, this method returns the first element found.




Note: The capitalization of "Id" in the name of this method must be correct for the code to function; getElementByID() is not valid and will not work, however natural it may seem.



The ID of the element to locate. The ID is a case-sensitive string which is unique within the document fragment: only one element should have any given ID.

Return value

An Element object describing the DOM element object matching the specified ID, or null if no matching element was found in the document fragment.


Extend a list of elements

In this example, the document contains a list with a single item Cherry. We also create a document fragment containing four more items, Apple, Orange, Banana, and Melon.

We then log the result of using getElementById() to look for Apple and Cherry in the document and in the fragment. At this point, Cherry appears only in the document while Apple appears only in the fragment.

If you click "Add fragment to document", we append the fragment to the list inside the document, and again log the result of looking for both Apple and Cherry in the document and in the fragment. This time, both Apple and Cherry appear in the document, and neither appear in the fragment.

This is because appending a fragment to a document moves the fragment's nodes into the DOM, leaving behind an empty DocumentFragment.



<button id="add">Add fragment to document</button>
<button id="reset">Reset example</button> <br />
List content:
  <li id="Cherry">Cherry</li>
Fragment content:
<ul id="fragment"></ul>
Current status:
<pre id="log" />



// Create the document fragment with its initial content
const fragment = new DocumentFragment();
["Apple", "Orange", "Banana", "Melon"].forEach((fruit) => {
  const li = document.createElement("li");
  li.textContent = fruit;
  li.id = fruit;

// When the button is clicked, add the fragment to the list
document.getElementById("add").addEventListener("click", () => {

// Log the results of both getElementById()
function displayStatus() {
  const log = document.getElementById("log");
  log.textContent = "";
  ["Apple", "Cherry"].forEach((id) => {
    log.textContent += `document.getElementById("${id}") ${
      document.getElementById(id) ? "Yes" : "No"
    log.textContent += `fragment.getElementById("${id}") ${
      fragment.getElementById(id) ? "Yes" : "No"

  // Empty the fragment viewer and fill it with the current content
  const fragmentViewer = document.getElementById("fragment");
  while (fragmentViewer.hasChildNodes()) {
  for (entry of fragment.children) {

// Log the initial state

// Hook the reset button
document.getElementById("reset").addEventListener("click", () => {



Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
getElementById 36 17 28 No 23 9 37 36 28 24 9 3.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.