I want to create a button that produces a 3x3 HTML table meant for a card game, where each cell corresponds to one single image. For the time being, I'm mainly focusing on filling individual cells with a card number taken from an array element, using a step-by-step rationale. This is what the final result should look like in HTML, derived from the produced script;
<table style="width:100%"> <tr> <td><img src="1.png"></td> <td><img src="2.png"></td> <td><img src="3.png"></td> </tr> <tr> <td><img src="4.png"></td> <td><img src="5.png"></td> <td><img src="6.png"></td> </tr> <tr> <td><img src="7.png"></td> <td><img src="8.png"></td> <td><img src="9.png"></td> </tr> </table> This is what I'm currently working at, although with not much success
<!DOCTYPE html> <html> <body> <table id="1"> </table> <button onclick="createTable()">Create</button> <script> function createTable() { var deck = [1,2,3,4,5,6,7,8,9]; function shuffle(o) { //v1.0 for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; } shuffle(deck); document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" + "<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" + "<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>" + ; } </script> </body> </html> Note The 9-card deck is shuffled using a pretty basic and self-explanatory function.
Edit. My final version, which is returning broken image.
<!DOCTYPE html> <html> <body> <table id="1"> </table> <button onclick="createTable()">Create</button> <script> function createTable() { var deck = [1,2,3,4,5,6,7,8,9]; function shuffle(o) { for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; } shuffle(deck); document.getElementById("1").innerHTML = "<tr><td><img src=" + deck[0] + ".png'></td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" + "<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" + "<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>" ; } </script> </body> </html>