swap image problem

Discussion in 'JavaScript' started by mirosoft1, Oct 8, 2008.

  1. #1
    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
     
    mirosoft1, Oct 8, 2008 IP
  2. kburb23

    kburb23 Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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?
     
    kburb23, Oct 8, 2008 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    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)
     
    dimitar christoff, Oct 8, 2008 IP