当element是表单的一部分时,document.getElementById返回null

I have php file that has some javascript inside. Consider the following javascript code, which prints null to the console (line 124 in the file):

console.log(document.getElementById('d_min')); 

Below the script, I have the following form (line 140 in the file):

<FORM name="DataCollect" ACTION="save_phase1.php" method="post">
    <input type="hidden" name="d_min" id="d_min" value=""/>
</FORM>

Why is it that it could be null? Why can't it find that element?


I FIXED THE PROBLEM:

I put the entire <form> as a document.write statement after the existing document.write statements I had.

The DOM is not fully loaded by the time the script executes. Move your script below that form and see if you get better results.

Wrap the JavaScript code in window.onload function.

I have no idea why it's not grabbing the field, but try giving the form an id foo and using

document.getElementById('foo').d_min

instead. At least it will narrow the source of the problem.