documnet.getElementById does not fetch the value . what is going wrong?
<p>i am sure this is quite a numb question to ask and most probably the most basic one. i am just a starter for JS.</p>

<p>i am trying to access the value of input field by document.getElementById and it is returning null to me i am not sure why here is the code.</p>

<pre><code>&lt;html&gt;
    &lt;head&gt;
        &lt;script type="text/javascript"&gt;
            var name = document.getElementById("e_name");
            alert(name);
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action="" method="post"&gt;
            &lt;input type="text" name="name" id="e_name" value="Enter your Name"/&gt;
            &lt;input type="submit" name="submit"/&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>the following code prints the value null in alert box. what is wrong?</p>

<p>Update :</p>

<p>When i use the following code.</p>

<pre><code>&lt;html&gt;
    &lt;head&gt;

    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action="" method="post"&gt;
            &lt;input type="text" name="name" id="name" value="Enter your Name"/&gt;
            &lt;input type="submit" name="submit"/&gt;
        &lt;/form&gt;
        &lt;script type="text/javascript"&gt;
            var name = document.getElementById('name').value;
            alert(name);
        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>it prints Enter your Name but if i change the value it does not print the changed value. i would want to perform the following for validation purpose</p>

<p>a) holds the value of e_name in a javascript variable in the head tag</p>

<p>b) so that i should be able to process it for validation.</p>

<p>how do i do it?</p>
