Document.form1 is undefined

It’s been a long night and I’m very frustrated.

This one is driving me crazy. I know it is something simple, I just can’t find it.

<html>
<head>
    <script language="JavaScript">
        alert(document.form1.tp.value);
    </script>
</head>
<body>
        <form name="form1" >
        <input type="text" name="tp" value="3">
        </form>
</body>
</html>

I get:

document.form1 is undefined

:sick::sick::sick:

When your script runs, this is the content of your document:

<html>
<head>
    <script language="JavaScript">
        alert(document.form1.tp.value);
    </script>

Yep, yep.

While trying to debug and reduce this to the smallest piece I could test I broke it further, :frowning:

Changing it to a function allowed me to verify it is returning the correct value. Now to begin adding the other pieces in until it breaks. :eek:

<html>
<head>
    <script language="JavaScript">
        function gettp() {
            alert(document.form1.tp.value);
        }
    </script>
</head>
<body>
        <form name="form1" >
        <input type="text" name="tp" value="3">
        <button onclick="gettp()" type="button">Done</button>
        </form>
</body>
</html>

What Logic Ali was getting at, is that when while a page is being loaded a script is run, nothing exists below the script.

The standard solution to this is to put the script just before the </body> tag.


<html>
<head>
</head>
<body>
    <form name="form1" >
        <input type="text" name="tp" value="3">
    </form>
    <script language="JavaScript">
        alert(document.form1.tp.value);
    </script>
</body>
</html>