The code in itself is correct.
You must just place it after the element that you are trying to manipulate.
<input type="text" id="name">
var a = document.getElementById("name");
a.value = "some value";
A best practice is to put all JS at the bottom of the page just before the closing body tag, then you avoid this problem altogether