JavaScript getElementById() – Select an Element By Id

Through this JavaScript getElementById example we will show you how to get or select an element by id in javascript, More then times we needs the getElementById() method if we are using javascript, Mostally as a web developer we are using javascript framework or jquery for client side works.

Here we will learn how to use the JavaScript getElementById() to select an element by a specified id of a div which will returns the element that has the ID attribute with the specified value.


Here the below code get the element where the id is ‘demoId’,

let element = document.getElementById("demoId");

Full Example for Get Element By Id Javascript

Here we are going to share the full example for javascript getElementById.

<!DOCTYPE html>

<p id="demoId">Get id element here.</p>

<button onclick="getElement()">Get Element</button>


getElement() {

let element = document.getElementById("demoId");



Here you will find the element using console.

<p id="demoId">Get id element here.</p>

Note: If multiple elements share the same id, even though it is invalid, the getElementById() returns the first element it encounters.

So, today we learn Get Element by ID in Javascript Example easy way with example steps, If you have any questions let me know in comments.

1 thought on “JavaScript getElementById() – Select an Element By Id”

Leave a Comment