Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you have the html <code>&lt;form id="formID"...</code> 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. </p> <p>There are many ways to achieve this. You can put all your JS near the <code>&lt;/body&gt;</code> tag, you can use <a href="http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS" rel="nofollow">Google's suggested deferred javascript loading practice</a> which appends all your JS to the <code>&lt;head&gt;</code> from the bottom of the body.</p> <p>This is one reason JQuery is so handy, because you can wrap your code in a <code>$(document).ready(function(){});</code> 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 <code>doScrollCheck()</code> 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. </p> <p>JQuery's DOM ready check: </p> <pre><code>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; })(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload