Search code examples
javascriptdomgetelementbyid

Is null after getElementById?


Very simple error here, I'm sure, but basically I have a form with id formID and im calling it like so:

<script type="text/javascript">
    function redvalidate() {
        var form = document.getElementById("formID")
        var fields = form.getElementsByClassName("validate"),

And I'm getting the error: form is null

Can anyone spot the error?

(PS calling it onsubmit of a form)

UPDATE Ok so basically i have two onsubmit for one form which oviously doesnt work. So what I am doing is calling this function from within another...

Heres the form tag:

<form name="purchaseform" id="formID" onSubmit="RememberFormFields('purchaseform','name,email,ship_to,phone_extension,pi_name');" action="process.php" method="post" enctype="multipart/form-data">

And heres RememberFormFields:

    <script type="text/javascript">
function RememberFormFields(form,list)
{
redvalidate()

...etc... rememberformfields function ...et.c..

Solution

  • Assuming you have the html <form id="formID"... somewhere on your page, you simply need to defer the loading of your javascript until after the DOM is ready. You're trying to get the ID from an element that hasn't been applied to the DOM yet and therefore JavaScript sees it as NULL.

    There are many ways to achieve this. You can put all your JS near the </body> tag, you can use Google's suggested deferred javascript loading practice which appends all your JS to the <head> from the bottom of the body.

    This is one reason JQuery is so handy, because you can wrap your code in a $(document).ready(function(){}); block so any DOM-manipulating scripts will always work because they wait until the DOM is created before they fire. It achieves this by looking for the "DOMContentLoaded" event in modern browsers and has a doScrollCheck() function for IE which tries to scroll the body over and over - if it is scrollable, they know it's good to go. You can easily replicate this practice if you don't want to load JQuery just for this.

    JQuery's DOM ready check:

    if ( document.addEventListener ) {
        DOMContentLoaded = function() {
            document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
            jQuery.ready();
        };
    
    } else if ( document.attachEvent ) {
        DOMContentLoaded = function() {
            // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
            if ( document.readyState === "complete" ) {
                document.detachEvent( "onreadystatechange", DOMContentLoaded );
                jQuery.ready();
            }
        };
    }
    
    // The DOM ready check for Internet Explorer
    function doScrollCheck() {
        if ( jQuery.isReady ) {
            return;
        }
    
        try {
            // If IE is used, use the trick by Diego Perini
            // http://javascript.nwbox.com/IEContentLoaded/
            document.documentElement.doScroll("left");
        } catch(e) {
            setTimeout( doScrollCheck, 1 );
            return;
        }
    
        // and execute any waiting functions
        jQuery.ready();
    }
    
    return jQuery;
    
    })();