html - How to randomly move an image around a table in Javascript -


i have image in cell of table , want move 1 cell @ random. planning on using setinterval , math.random() move image around randomly every 2 seconds cannot image move around @ all

<html> <head>     <style>         tr { width: 300px; height: 100px }         td { width: 100px; height: 100px }         img { width: 100px; height: 100px }     </style>         <script>              function moveimgrandomly()             {               }         </script> </head> <body bgcolor="lightblue">     <table border=1 id="mytable">         <tr>             <td></td>             <td></td>             <td><img src="http://graemehobbs93.files.wordpress.com/2012/01/ape-1.jpg" id="img"></td>         </tr>         <tr>             <td></td>             <td></td>             <td></td>         </tr>         <tr>             <td></td>             <td></td>             <td></td>         </tr>     </table> </body> 

this have far works. have been trying hours image move , not. cannot use jquery.

this should it.

var img = document.getelementbyid("img");  var tds = document.getelementsbytagname("td");    setinterval(function(){  	var randomnumber = math.floor(math.random() * tds.length);  	tds[randomnumber].appendchild(img);  }, 2000);
tr { width: 300px; height: 100px }  td { width: 100px; height: 100px }  img { width: 100px; height: 100px }
<body bgcolor="lightblue">      <table border=1 id="mytable">          <tr>              <td></td>              <td></td>              <td><img src="http://graemehobbs93.files.wordpress.com/2012/01/ape-1.jpg" id="img"></td>          </tr>          <tr>              <td></td>              <td></td>              <td></td>          </tr>          <tr>              <td></td>              <td></td>              <td></td>          </tr>      </table>  </body>


Comments