document.getElementById() not working - maybe because page is not loaded yet?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element">Why does jQuery or a DOM method such as `getElementByID` not find the element?</a>  </p>
</blockquote>



<p>Before the HTML tag I run some php code. During this I need to include some javascript lines like</p>

<pre><code>..php code...
..javascript code...
..php code..
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="file" name="file" id="file"&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>In the javascript code I want to hide the file element.</p>

<p>This doesn't do anything:</p>

<pre><code>..php code
?&gt;
&lt;SCRIPT LANGUAGE="JavaScript"&gt;
 document.getElementById('file').style.display = 'none';
&lt;/script&gt;
&lt;?php
</code></pre>

<p>...php code</p>

<p>If I put an alert there, it is <strong>displayed</strong>.</p>

<p>If I put that line into a function in the <code>&lt;head&gt;&lt;/head&gt;</code> block and call it from e.g. an onClick event of a button, the file element becomes hidden, so the line is fine. Maybe <code>document.getLementByID</code> is invalid outside the html tag? Or the problem is maybe that the page is not loaded yet so there is no element with that id? If this is the case, what can I do?</p>

<p><strong>SOLUTION</strong></p>

<p>I added a variable in the php code as $int = 1; (and $int = 0; somewhere else, but still before the page is loaded).
Then in the <code>&lt;head&gt;&lt;/head&gt;</code> block:</p>

<pre><code>window.onload = selectChoose;
function selectChoose() 
{
    if (&lt;?php echo $int ?&gt; == 0) {
        showChoose();
    }
    else {
        hideChoose();
    }
}

function showChoose() 
{
    document.getElementById('file').style.display = 'inline';
}

function hideChoose() 
{
    document.getElementById('file').style.display = 'none';
}
</code></pre>
