need help with getElementsByTagName

Discussion in 'JavaScript' started by Abh, Aug 11, 2010.

  1. #1
    This is the code:
    
    onload = function() {
    	onresize = tv.resize;
    	/* ==== build grid ==== */
    	var img = document.getElementById('bankImages').getElementsByTagName('img');
    	var structure = [];
    	for (var i = -300; i <= 300; i += 120)
    		for (var j = -300; j <= 300; j += 120)
    			structure.push({ img:img[0], x:i, y:j, z:0, sw:.5, sh:.5 });
    	/* ==== let's go ==== */
    	tv.init(structure, 350, -200, .005, .0025);
    }
    
    Code (markup):
    
    <div id="bankImages">
    	<img alt="" src="photo3D-2_data/1.png" />
    	<img alt="" src="photo3D-2_data/2.png" />
    	<img alt="" src="photo3D-2_data/3.png" />
    	<img alt="" src="photo3D-2_data/4.png" />
    	<img alt="" src="photo3D-2_data/5.png" />
    	<img alt="" src="photo3D-2_data/6.png" />
    	<img alt="" src="photo3D-2_data/7.png" />
    	<img alt="" src="photo3D-2_data/8.png" />
    </div>
    
    Code (markup):
    It's a part of http://www.dhteumeuleu.com/wanna-tell-her/
    But it only gets the first image, not all of them. I can change the key in img[0] and get another pic, but how do i get them all?
     
    Abh, Aug 11, 2010 IP
  2. gumape

    gumape Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The getElementsByTagName method returns an array. The length property returns the size of an array and the elements of an array can be retrieved with the [] operator.

    The following example iterates through the images:
    var imagesDiv = document.getElementById('bankImages');
    var images = imagesDiv.getElementsByTagName('img');
    for (var idx = 0; idx < images.length; idx++) {
    	structure.push ( {img: images[idx], x:0, y:0, z:0, sw:.5, sh:.5} );
    }
    Code (markup):

    If you want to use it in your code, one possible solution is this:
    onload = function() {
    	onresize = tv.resize;
    
    	/* ==== build grid ==== */
    	var imagesDiv = document.getElementById('bankImages');
    	var images = imagesDiv.getElementsByTagName('img');
    	var structure = [];
    	for (var i = 0; i < 6; i++) {
    		for (var j = 0; j < 6; j++) {
    				// 36 grid points, but only 8 images
    			var imageIdx = (i + 6 * j) % images.length;
    
    			structure.push ( {img:images[imageIdx], x:-300 + 120 * i, y:-300 + 120 * j, z:0, sw:.5, sh:.5} );
    		}
    	}
    
    	/* ==== let's go ==== */
    	tv.init(structure, 350, -200, .005, .0025);
    }
    Code (markup):

    If you need further details about the getElementsByTagName method or arrays, the following pages will be useful:
    getElementsByTagName method,
    Array object.
     
    gumape, Aug 11, 2010 IP
  3. Abh

    Abh Active Member

    Messages:
    162
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Wow.

    I still have some modifications to do on the script, but your solution works perfectly.

    Also, thank you for the links, i will check them out.
     
    Abh, Aug 11, 2010 IP