hi i made this code to swap image and then i call the function on php code but on while loop the image is repeated but when i click the first image only do swap i want all the images do swap <script type="text/javascript"> <!-- var oImgs = []; oImgs[0] = "b_no.JPG" oImgs[1] = "b_ok.JPG" for(var i=0;i<oImgs.length;i++){ var imgs = new Image(); imgs.src =oImgs; } var x = 1; function swapImg(){ var doc = document.getElementById("swap"); doc.src = oImgs[x]; if(x<oImgs.length-1){ x ++; }else{ x = 0; } } //--> </script> php code::::::: while ($threads = mysql_fetch_array($tr)) { echo "<tr>"; echo "<td> <img id='swap' src='b_no.JPG' onclick='swapImg();' /> can anyone help me plz
Can you describe more clearly what is happening? Your sentence is not very clear. The code looks basically reasonably, but it's not clear what you *want* it to do?
er. you have id 'swap'. you can only have 1 item with this id, more than 1 just won't work. try to increment the element id: $done = 1; while ($threads = mysql_fetch_array($tr)) { echo "<tr>"; echo "<td> <img id='swap{$done}' src='b_no.JPG' onclick='swapImg();' />"; // ... $done++; ... } <script> function swapImg(){ var doc = document.getElementById("swap"+x); // notice +x doc.src = oImgs[x]; if(x<oImgs.length-1){ x ++; } else { x = 0; } } </script> PHP: not well organised anyway... consider using swapImg(this, index)