Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Short and simple:</strong> Because the elements you are looking for do not exist in the document (yet).</p> <hr> <p><em>For the remainder of this answer I will use <code>getElementById</code> as example, but the same applies to <a href="https://developer.mozilla.org/en-US/docs/DOM/element.getElementsByTagName" rel="noreferrer"><code>getElementsByTagName</code></a>, <a href="https://developer.mozilla.org/en-US/docs/DOM/Document.querySelector" rel="noreferrer"><code>querySelector</code></a> and any other DOM method that selects elements.</em></p> <p><strong>Possible Reasons</strong></p> <p>There are two reasons why an element might not exist:</p> <ol> <li><p>An element with the passed ID really does not exist in the document. You should double check that the ID you pass to <code>getElementById</code> really matches an ID of an existing element in the (generated) HTML and that you have not <em>misspelled</em> the ID (IDs are <em>case-sensitive</em>!).</p> <p>Incidentally, in the <a href="http://caniuse.com/queryselector" rel="noreferrer">majority of contemporary browsers</a>, which implement <code>querySelector()</code> and <code>querySelectorAll()</code> methods, CSS-style notation is used to retrieve an element by its <code>id</code>, for example: <code>document.querySelector('#elementID')</code>, as opposed to the method by which an element is retrieved by its <code>id</code> under <code>document.getElementById('elementID')</code>; in the first the <code>#</code> character is essential, in the second it would lead to the element not being retrieved.</p></li> <li><p>The element does not exist <em>at the moment</em> you call <code>getElementById</code>.</p></li> </ol> <p>The latter case is quite common. Browsers parse and process the HTML from top to bottom. That means that any call to a DOM element which occurs before that DOM element appears in the HTML, will fail.</p> <p>Consider the following example:</p> <pre><code>&lt;script&gt; var element = document.getElementById('my_element'); &lt;/script&gt; &lt;div id="my_element"&gt;&lt;/div&gt; </code></pre> <p>The <code>div</code> appears <em>after</em> the <code>script</code>. At the moment the script is executed, the element does not exist <em>yet</em> and <code>getElementById</code> will return <code>null</code>.</p> <p><strong>jQuery</strong></p> <p>The same applies to all selectors with jQuery. jQuery won't find elements if you <em>misspelled</em> your selector or you are trying to select them <em>before they actually exist</em>.</p> <p>An added twist is when jQuery is not found because you have loaded the script without protocol and are running from file system:</p> <pre><code>&lt;script src="//somecdn.somewhere.com/jquery.min.js"&gt;&lt;/script&gt; </code></pre> <p>this syntax is used to allow the script to load via HTTPS on a page with protocol https:// and to load the HTTP version on a page with protocol http://</p> <p>It has the unfortunate side effect of attempting and failing to load <code>file://somecdn.somewhere.com...</code></p> <hr> <p><strong>Solutions</strong></p> <p>Before you make a call to <code>getElementById</code> (or any DOM method for that matter), make sure the elements you want to access exist, i.e. the DOM is loaded.</p> <p>This can be ensured by simply putting your JavaScript <em>after</em> the corresponding DOM element</p> <pre><code>&lt;div id="my_element"&gt;&lt;/div&gt; &lt;script&gt; var element = document.getElementById('my_element'); &lt;/script&gt; </code></pre> <p>in which case you can also put the code just before the closing body tag (<code>&lt;/body&gt;</code>) (all DOM elements will be available at the time the script is executed).</p> <p>Other solutions include listening to the <a href="https://developer.mozilla.org/en-US/docs/DOM/window.onload" rel="noreferrer"><code>load</code> <em><sup>[MDN]</sup></em></a> or <a href="https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/DOMContentLoaded_%28event%29" rel="noreferrer"><code>DOMContentLoaded</code> <em><sup>[MDN]</sup></em></a> events. In these cases it does not matter where in the document you place the JavaScript code, you just have to remember to put all DOM processing code in the event handlers.</p> <p>Example:</p> <pre><code>window.onload = function() { // process DOM elements here }; // or // does not work IE 8 and below document.addEventListener('DOMContentLoaded', function() { // process DOM elements here }); </code></pre> <p>Please see the <a href="http://www.quirksmode.org/js/introevents.html" rel="noreferrer">articles at quirksmode.org</a> for more information regarding event handling and browser differences.</p> <p><strong>jQuery</strong></p> <p>First make sure that jQuery is loaded properly. <a href="http://www.netmagazine.com/tutorials/javascript-debugging-beginners" rel="noreferrer">Use the browser's developer tools</a> to find out whether the jQuery file was found and correct the URL if it wasn't (e.g. add the <code>http:</code> or <code>https:</code> scheme at the beginning, adjust the path, etc.)</p> <p>Listening to the <code>load</code>/<code>DOMContentLoaded</code> events is exactly what jQuery is doing with <a href="http://api.jquery.com/ready/" rel="noreferrer"><code>.ready()</code> <em><sup>[docs]</sup></em></a>. All your jQuery code that affects DOM element should be inside that event handler.</p> <p>In fact, the <a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery" rel="noreferrer">jQuery tutorial</a> explicitly states:</p> <blockquote> <p>As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.</p> <p>To do this, we register a ready event for the document.</p> <pre><code>$(document).ready(function() { // do stuff when DOM is ready }); </code></pre> </blockquote> <p>Alternatively you can also use the shorthand syntax:</p> <pre><code>$(function() { // do stuff when DOM is ready }); </code></pre> <p>Both are equivalent.</p>
    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. 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