How to Get Element by ID in JavaScript

To get an element by its ID attribute in JavaScript, use the document.getElementById() method and pass the ID as the first argument.

 

Let's try this out with a simple example where we will get an element by ID then get the text inside it.

 

var elem = document.getElementById('content');

console.log(elem.innerText);
Hello!