SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Fiska's Avatar
    Join Date
    Jul 2004
    Location
    Dardania - Ancient Illyria
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry why this simple script it won't work??

    ok, I gave up, javascript definitely is driving me crazy!

    why on earth this script wont work??? I basically want to move all images from one div to another. Simple as that.

    here is the code:


    Code:
    <html>
    <head>
    <script type="text/javascript">
    <!--
    
    function replace()
    {
    var k1 = document.getElementById("foto1");
    var k2 = document.getElementById("foto2");
    
    	
    	var foto = k1.getElementsByTagName('img');
    	
    		for(i = 0; i < foto.length; i++)
    		{
    			k2.appendChild(foto[i]);
    			
    		}
    
    	// if the browser can deal with DOM, call the function onload		
    		if(document.getElementById && document.createTextNode)
    		{
    			window.onload=replace;
    		}
    }//end replace
    //-->
    </script>
    
    </head>
    <body>
    
    <div id="foto1" style="border: solid 1px red">
    <img src="img/a1.jpg" height="150" width="150" alt="" /><br/>
    <img src="img/a2.jpg" height="150" width="150" alt="" /><br/>
    <img src="img/a2.jpg" height="150" width="150" alt="" /><br/>
    <img src="img/a2.jpg" height="150" width="150" alt="" /><br/>
    </div>
    
    <br/>
    
    
    <div id="foto2" style="border: solid 1px #000">
    
    </div>
    
    
    </body>
    
    <html>
    thanx in advance
    let's make things easier

  2. #2
    SitePoint Zealot Fiska's Avatar
    Join Date
    Jul 2004
    Location
    Dardania - Ancient Illyria
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok one problem I figured it out, it was onload part standing in the function, I did remove it out, but still only two images are being removed and two remaining in the old div ? why ?

    Code:
    // if the browser can deal with DOM, call the function onload		
    		if(document.getElementById && document.createTextNode)
    		{
    			window.onload=replace;
    		}
    let's make things easier

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    It's because you're comparing the loop against the size of the collection that elements are being taken out of. Each time you loop through, the loop counter is increasing, and the size of the foto collection is decreasing. Eventually they meet in the middle and the loop ends.

    You could either place the length of the collection in a variable outside of the loop, and compare against that, or use a different looping technique with the while loop instead.

    Code javascript:
    while (foto.length)
    {
    	k2.appendChild(foto[0]);
    }

    Or if you want them backwards

    Code javascript:
    while (foto.length)
    {
    	k2.appendChild(foto[foto.length - 1]);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot Fiska's Avatar
    Join Date
    Jul 2004
    Location
    Dardania - Ancient Illyria
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, it is working now.
    let's make things easier


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •