Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple "document.getElementById" scripts doesn't work
    text
    copied!<p>Multiple IDs works fine when I'm using HTML #1 but doesn't work when I'm using HTML #2. Chrome web console shows </p> <p><code>&gt;&gt; "Uncaught TypeError: Cannot call method 'setAttribute' of null ".</code></p> <p>In html#2 I removed </p> <p><code>&lt;div id="0001"&gt;&lt;a href="#""&gt;LOGIN&lt;/a&gt;&lt;/div&gt;</code>, and the browsers do not work anymore. </p> <p><strong>HTML #1</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="0001"&gt;&lt;a href="#""&gt;LOGIN&lt;/a&gt;&lt;/div&gt; &lt;div id="0002"&gt;&lt;a href="#""&gt;GO&lt;/a&gt;&lt;/div&gt; &lt;div id="0003"&gt;&lt;a href="#""&gt;VISIT&lt;/a&gt;&lt;/div&gt; &lt;div id="0004"&gt;&lt;a href="#""&gt;EXPLORE&lt;/a&gt;&lt;/div&gt; &lt;div id="0005"&gt;&lt;a href="#""&gt;FUNNY PICS&lt;/a&gt;&lt;/div&gt; &lt;script&gt; var element = document.getElementById("0001"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/1.html\''); var element = document.getElementById("0002"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/2.html\''); var element = document.getElementById("0003"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/3.html\''); var element = document.getElementById("0004"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/4.html\''); var element = document.getElementById("0005"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/5.html\''); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>HTML #2</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="0002"&gt;&lt;a href="#""&gt;GO&lt;/a&gt;&lt;/div&gt; &lt;div id="0003"&gt;&lt;a href="#""&gt;VISIT&lt;/a&gt;&lt;/div&gt; &lt;div id="0004"&gt;&lt;a href="#""&gt;EXPLORE&lt;/a&gt;&lt;/div&gt; &lt;div id="0005"&gt;&lt;a href="#""&gt;FUNNY PICS&lt;/a&gt;&lt;/div&gt; &lt;script&gt; var element = document.getElementById("0001"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/1.html\''); var element = document.getElementById("0002"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/2.html\''); var element = document.getElementById("0003"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/3.html\''); var element = document.getElementById("0004"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/4.html\''); var element = document.getElementById("0005"); element.setAttribute('onclick', 'window.location.href=\'http://site.com/5.html\''); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

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