JavaScript: getElementById

getElementById() finds the element whose id matches a string, so a script can read or update that element.

What you will learn

Minimal example

<p id="message">Waiting...</p>
<script>
  const message = document.getElementById('message');
  if (message) message.textContent = 'Ready';
</script>

The id should be unique in the document. Use textContent for plain text so a value is not interpreted as HTML.

Common mistakes