
What is the use of document.getElementById method in JavaScript? Please provide a short example of this method in HTML.

JavaScript Tutorial - document.getElementById
document.getElementById is a method in JavaScript, which is used to accesses the first element with the specified id. Where ID is required. We can take a simple example of displaying date in a WebPage on button Click.
<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>
<h1>Date Example in JavaScript</h1>
<p id="demo">click to Display Date.</p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.