html - Randomly inserting images into table using Javascript -


i'm new javascript , i'm creating game user re-arranges table of pictures clicking. images scrambled pieces of larger image user has sort out.

the user clicks on 1 of images in table, clicks image in table, , swap positions. have use javascript this, ive created 3x4 table , filled 12 "partial" images need re-arranged.

each time page loads, need have images put in random cells each game, user has sort images in different way.

to this, use diffimage() function picks random image array of images have. if 1 of images named "mario01.jpg", function takes image , makes <img src="mario01.jpg" />.

my diffimage() function takes image selected out of array image won't inserted second time table.

i use diff image function each image give random source.

however, when load page, no images show , error says, [error] typeerror: 'null' not object (evaluating 'document.getelementbyid("image1").src = diffimage()') global code (picturegame.html, line 49)

i can't see im doing wrong here. not giving picture source, , inserting table? here code have done far:

        <head>         <link rel="stylesheet" href="css.css" type="text/css"/>         <title>picture scramble game</title>         </head>         <script>         var imagelist = ["mario01.jpg", "mario02.jpg", "mario03.jpg", "mario04.jpg", "mario05.jpg", "mario06.jpg", "mario07.jpg", "mario08.jpg", "mario09.jpg", "mario10.jpg", "mario11.jpg", "mario12.jpg"];           function diffimage() {         var output = '';         whichimage = math.floor(math.random()*imagelist.length);         output = imagelist[whichimage];         imagelist.splice(whichimage,1);         return output;     }  document.getelementbyid("image1").src = diffimage(); document.getelementbyid("image2").src = diffimage(); document.getelementbyid("image3").src = diffimage(); document.getelementbyid("image4").src = diffimage(); document.getelementbyid("image5").src = diffimage(); document.getelementbyid("image6").src = diffimage(); document.getelementbyid("image7").src = diffimage(); document.getelementbyid("image8").src = diffimage(); document.getelementbyid("image9").src = diffimage(); document.getelementbyid("image10").src = diffimage(); document.getelementbyid("image11").src = diffimage(); document.getelementbyid("image12").src = diffimage();           </script>         <body onload="addeventlisteners();">         </script>         <div id="table">         <table>             <tr><td><img class="element" id="image1" src="mario02.jpg" /></td>             <td><img class="element" id="image2" src="mario02.jpg" /></td>             <td><img class="element" id="image3" src="mario01.jpg" /></td>             </tr>             <tr><td><img class="element" id="image4" src="mario05.jpg" /></td>             <td><img class="element" id="image5" src="mario04.jpg" /></td>             <td><img class="element" id="image6" src="mario06.jpg" /></td>             </tr>             <tr><td><img class="element" id="image7" src="mario09.jpg" /></td>             <td><img class="element" id="image8" src="mario08.jpg" /></td>             <td><img class="element" id="image9" src="mario07.jpg" /></td>             </tr>             <tr><td><img class="element" id="image10" src="mario11.jpg" /></td>             <td><img class="element" id="image11" src="mario10.jpg" /></td>             <td><img class="element" id="image12" src="mario12.jpg" /></td></tr>         </table>         </div>         </body> 

your code running before elements available on page.

move script bottom, before closing body-tag.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -