Use of document.getElementById in JavaScript

user2931781 picture user2931781 · Oct 29, 2013 · Viewed 112.3k times · Source

Can someone explain what the document.getElementById("demo") line does in the example below?

I understand getElementById gets the id of demo but the id is <p id="demo"></p> What exactly is <p id="demo"></p> doing in this code?

document.getElementById("age") is clear as it gets the id of age which is the input.

Answer

BoltClock picture BoltClock · Oct 29, 2013

You're correct in that the document.getElementById("demo") call gets you the element by the specified ID. But you have to look at the rest of the statement to figure out what exactly the code is doing with that element:

.innerHTML=voteable;

You can see here that it's setting the innerHTML of that element to the value of voteable.