Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your example demonstrates when you have a reference to the element you want to control. If you already have a reference to the element, there is no reason to use <code>document.getElementById</code> to obtain a reference to it (you already have one):</p> <pre><code>&lt;span id="mySpan" onclick="test(this);"&gt;TEST&lt;/span&gt; &lt;script type="text/javascript"&gt; function test(el) { // el is a reference to an element, you can use it directly el.style.display = 'none'; // to demonstrate that the element you retrieve from gEBI // with el.id is the same as your original reference var elAgain = document.getElementById(el.id); alert(elAgain === el); // "true" } &lt;/script&gt; </code></pre> <p>The place where you might want to use <code>document.getElementById</code> is when you want to control another element to which you don't already have a reference:</p> <pre><code>&lt;span id="mySpan" onclick="test(this, 'myDiv');"&gt;TEST&lt;/span&gt; &lt;div id="myDiv" style="display:none;"&gt;DIV REVEALED&lt;/div&gt; &lt;script type="text/javascript"&gt; function test(el, otherElId) { // el is a reference to an element, you can use it directly el.style.display = 'none'; // otherElId is a string containing the id of // the element you want to control var theDiv = document.getElementById(otherElId); theDiv.style.display = ''; } &lt;/script&gt; </code></pre> <p>Note that there is rarely (if ever) a reason to do something like the following:</p> <pre><code>&lt;span id="mySpan" onclick="test('mySpan');"&gt;TEST&lt;/span&gt; &lt;script type="text/javascript"&gt; function test(theElId) { var el = document.getElementById(theElId); el.style.display = 'none'; } &lt;/script&gt; </code></pre> <p>If the function is being fired from an element event, you can just pass <code>this</code> (as per my first example), rather than passing the id of the element the event is happening on.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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