JavaScript getElementById(): Find an element by its ID

document.getElementById("notice") finds the element whose ID is exactly notice. It returns null for no match and is case-sensitive. Pass the ID alone, without the # used for CSS selectors.

Select one element with a literal ID

const notice = document.getElementById("notice");

if (notice !== null) {
  notice.textContent = "처리가 완료되었습니다.";
}

getElementById() returns an element in the Document Object Model (DOM), the browser’s tree of nodes. Read its textContent if you need the text. The method name ends in Id. Use it on document, since an ordinary parent element does not provide this method.

Find, edit, and restore in a complete example

Save the sample in UTF-8 as get-element-by-id.html and open it. No library is required. The script follows the buttons and output element so their nodes already exist.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Find an element by ID</title>
</head>
<body>
<h1>Find an element by ID</h1>
<p id="notice">Initial notice</p>
<label for="lookup-id">ID to find</label>
<select id="lookup-id">
  <option value="notice">notice</option>
  <option value="Notice">Notice</option>
  <option value="#notice">#notice</option>
  <option value="missing">missing</option>
</select>
<button id="find" type="button">Find</button>
<button id="change" type="button">Change notice</button>
<button id="restore" type="button">Restore</button>
<pre id="output"></pre>
<script>
const notice = document.getElementById("notice");
const selectedId = document.getElementById("lookup-id");
const output = document.getElementById("output");
function findElement() {
  const element = document.getElementById(selectedId.value);
  if (element === null) {
    output.textContent = "Element not found.";
    return;
  }
  output.textContent = `Tag: ${element.tagName}\nText: ${element.textContent}`;
}
document.getElementById("find").addEventListener("click", findElement);
document.getElementById("change").addEventListener("click", () => {
  notice.textContent = "Updated notice";
  findElement();
});
document.getElementById("restore").addEventListener("click", () => {
  notice.textContent = "Initial notice";
  selectedId.value = "notice";
  findElement();
});
findElement();
</script>
</body>
</html>

The output starts with Tag: P and Text: Initial notice. Click Change notice to update the same element. Change the input to Notice or #notice and click Find to see a missing result. Restore resets both the text and ID choice. The function returns early on null so it never reads a missing element’s properties.

Trace why an expected element returns null

Compare the actual id with the lookup string first. Check case, spaces, and spelling. name or class alone will not match. If the script runs too early, move it after the markup or use defer on an external classic script. An inline script will not defer this way. A node created with createElement() also stays outside the document lookup until inserted.

<script src="app.js" defer></script>

Handle duplicate IDs and choose another selector

Keep IDs unique. If duplicates exist, the first match in tree order is returned, which may be the wrong element for your task. Use a class for repeated cards. querySelector() accepts a CSS selector and returns the first match, while querySelectorAll() returns all matches. To update the found element, see innerText, innerHTML, textContent.

const one = document.querySelector("#notice");
const cards = document.querySelectorAll(".card");

Look up punctuation in a detached fragment

Run the code in the example page’s Console. Use the literal ID result:2026 for direct lookup. Use CSS.escape() on the ID before prepending # to build a CSS selector, because a colon has a meaning in selector syntax.

{
  const fragment = document.createDocumentFragment();
  const title = document.createElement("p");
  title.id = "result:2026";
  fragment.append(title);

  console.log(fragment.getElementById(title.id) === title); // true
  console.log(fragment.querySelector("#" + CSS.escape(title.id)) === title); // true
  console.log(document.getElementById(title.id) === null); // true in the example
}

All three lines log true. The first two searches find the element inside the DocumentFragment. The document search finds nothing because the fragment was not inserted. If you already have a reference to the element, you can keep using that reference without another lookup.