1

So I've been at this for hours and I've been trying to do some homework in web development. I ended up quitting the original document since it is very large and distracting and just creating a barebones document trying to figure out why my form variable is null. The code I ended up writing is below:

HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="index.js"></script>
    </head>

    <body>
        <form id="mainForm">
            <fieldset>
                <legend>Form legend</legend>
                <input type="submit" />
            </fieldset>
        </form>
    </body>
</html>

JavaScript

var form = document.getElementById("mainForm");

So, can someone clear up why the form variable is coming back as null?

I am Root.
  • 131
  • 7
  • 2
    You try to get it before that `form` is rendered. Try put that ` – fuyushimoya Oct 24 '15 at 21:49
  • 2
    It looks like you're executing your JS code before the elements exist. Try moving your code to the end of the page (before the closing `` tag). – j08691 Oct 24 '15 at 21:49
  • 1
    Set your script at the bottom. It works fine. http://jsfiddle.net/d73h1k2v/ – Nicholas Hazel Oct 24 '15 at 21:49
  • 1
    [*Why does jQuery or a DOM method such as getElementById not find the element*'](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – adeneo Oct 24 '15 at 21:53
  • Thanks a lot guys. That cleared up my questions. – I am Root. Oct 24 '15 at 21:56

0 Answers0