Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't set property src of a null even though DOM is ready
    primarykey
    data
    text
    <p>So basically I have this table with six images in it for a nice image gallery. On the nav bar on the bottom I have a javascript function that is supposed to use an array of image file sources to change the individual tags of the image elements in the table when the user clicks. All of the javascript is supposed to execute when the DOM is ready. The problem is that when I try to click on the nav bar at the bottom I get the "cannot set src value of a null" which makes me thing that the array isn't working right or it isn't calling the elements the way it is supposed to. </p> <p>This is the PHP script that creates the table, pulls the images, and sticks them into the javascript array when the DOM is ready, I have a sneaking suspicion that part of the problem is that the code won't execute unless I include two <code>&lt;/script&gt;</code>'s after the part that should create the javascript array.</p> <pre><code>&lt;?php $directory = 'images/gallery/'; $files1 = scandir($directory); $length = count($files1); echo "&lt;script $( document ).ready(function() {"; $y = 2; for ($x = 0; $x &lt; $length-2; $x++) { echo "images[$x] = 'Images/gallery/$files1[$y]';"; $y= $y + 1; } echo "});"; echo"&lt;/script&gt;"; echo"&lt;/script&gt;"; echo "&lt;table&gt;&lt;tr&gt;"; for ($i = 2; $i &lt;= 7; $i++) { if ($i == 5) { echo "&lt;/tr&gt;&lt;tr&gt;"; } echo "&lt;td class='galleryImageWrapper'&gt;&lt;a href='images/gallery/$files1[$i]' data-lightbox='Gallery'&gt;&lt;img id='$i' src='images/gallery/$files1[$i]'&gt;&lt;/a&gt;&lt;/td&gt;"; } echo "&lt;/tr&gt;&lt;/table&gt;"; echo "&lt;div id='galleryNavWrapper'&gt;&lt;div onclick='advanceLeft()' id='leftbutton'&gt;&amp;#8656; &lt;/div&gt;&lt;ul id='galleryNav'&gt;"; for ($i = 1; $i &lt;= ($length / 6); $i++) { echo "&lt;li onclick='galleryNavButton(this.value)'&gt;$i&lt;/li&gt;"; } echo '&lt;/ul&gt;&lt;div id="rightButton" onclick = "advanceRight()"&gt;&amp;#8658;&lt;/div&gt;'; ?&gt; </code></pre> <p>This the HTML Code</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /&gt; &lt;title&gt;Perspective by Daniel Streich&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="scripts/css.css"&gt; &lt;link href="css/lightbox.css" rel="stylesheet"&gt; &lt;script src="js/jquery-1.10.2.min.js"&gt;&lt;/script&gt; &lt;script src="js/lightbox-2.6.min.js"&gt;&lt;/script&gt; &lt;link href="css/lightbox.css" rel="stylesheet" /&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; var i = 0; var images = new Array(); /* $( document ).ready(function() { document.write("well this be working"); });*/ function advanceLeft() { if (i == 0) { return false; } else { i = i - 6; for (var x = i; x &lt;= 5; x++) { document.getElementById(x).src = images[x]; return true; } } } function advanceRight() { if (i == images.length - 1) { return false; } else { i = i + 6; for (var x = i; x &lt;= 5; x++) { document.getElementById(x).src = images[x]; return true; } } } function galleryNavButton(newId) { i = i - ((i - newId) * 6); for (var x = i; i &lt;= 5; x++) { document.getElementById(x).src = images[x]; return true; } } &lt;/script&gt; &lt;?php $active = "Art"; include ('menu.php'); ?&gt; &lt;div class="wrapperRegular"&gt; &lt;p class="bigTitle"&gt; Perspective by Daniel &lt;/p&gt; &lt;p class='actualText'&gt; TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST &lt;/p&gt; &lt;?php include ('gallery.php'); ?&gt; &lt;br&gt; &lt;div id="footer"&gt; Copyright &amp;#169; Perspective by Daniel &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p></p> <p>If anyone can figure this out, I would be extremely thankful since I can't get it for the life of me.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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