I have this code:
var StateTextBox = document.getElementById("State");
function InitializeVP() {
StateTextBox.value = "Test"
}
It can’t find the StateTextBox as a variable… Does anyone know why?
When I do it like so:
function InitializeVP() {
document.getElementById("State").value = "Test"
}
It works. Can anyone explain to me what I am doing wrong please?
When are you attempting to set the StateTextBox variable? Chances are, it is attempting to be set in the head section, and in that case, the element will not be set.
Try putting it just before the closing
</body>tag, and it will probably work there (you can leave yourInitializeVP()function at the top.Even better? Use jQuery with the
$(document).ready, and you are pretty much guaranteed that your variable will be initialized.