DEV Community

Cover image for Understanding getElementById
Tushar
Tushar

Posted on

Understanding getElementById

The getElementById method is a fundamental JavaScript function used to select a single HTML element by its id attribute. It returns the element object representing the element whose id property matches the specified string.

Example Usage:

Let's say you have the following HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>
    <script src="script.js"></script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

In your script.js file, you can use getElementById to select the div and manipulate it:

const root = document.getElementById("root");
const Heading = document.createElement("h1");
Heading.innerHTML = "Hello World";
root.appendChild(Heading);

Enter fullscreen mode Exit fullscreen mode

When you open your HTML file in a browser, the div with the id root will now contain an h1 element with the text "Hello World".

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more