www.webdeveloper.com
Results 1 to 3 of 3

Thread: populate a div using array

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    44

    populate a div using array

    i've been trying to populate a div with multiple images with the help of an array.what could be the mistakes in here ?
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <style>
    *{
    	margin:0px;padding:0px;}
    
    #slider{
    	width:100%;
    	height:150px;
    	border:1px solid black;
    	position:absolute;
    	top:200px;
    }
    #slider img{
    	width:100px;
    	height:140px;
    	margin-left:2px;
    }
    </style>
    <script>
    function l(){
    var imag=["image1.jpg","image2.jpg","image3.jpg","image4.jpg"];
    var m=document.getElementById("slider");
     get_width=m.offsetWidth;
    var no_of_img=parseInt(get_width/(100));
    var i;
    for(i=0;i<no_of_img;i++){
    	m.appendChild += <img src="imag[i]" /> ;
    }
    
    }
    
    </script>
    
    
    
    </head>
    
    
    <body style="position:relative;">
    <div  id="board"></div><div id="imagebox"></div>
    <div id="slider"></div>
    <script>l();</script>
    </body>
    </html>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,674
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <style>
    *{
    	margin:0px;padding:0px;}
    
    #slider{
    	width:100%;
    	height:150px;
    	border:1px solid black;
    	position:absolute;
    	top:200px;
    }
    #slider img{
    	width:100px;
    	height:140px;
    	margin-left:2px;
    }
    </style>
    
    
    
    </head>
    
    
    <body style="position:relative;">
    <div  id="board"></div><div id="imagebox"></div>
    <div id="slider"></div>
    
    <script>
    function myfunction1(){ // function names must begin with a letter or $
     var imag=["http://www.vicsjavascripts.org.uk/StdImages/1.gif","http://www.vicsjavascripts.org.uk/StdImages/2.gif","http://www.vicsjavascripts.org.uk/StdImages/3.gif","http://www.vicsjavascripts.org.uk/StdImages/4.gif"];
     var m=document.getElementById("slider"),get_width=m.offsetWidth,i;
    
     var no_of_img=parseInt(get_width/(100));
     for(i=0;i<no_of_img;i++){ // syntacx for mixing string with varioble
    	m.innerHTML += '<img src="'+imag[i%imag.length]+'" />'; // as no_of_img is geater than imag.length modulus(%) is used to repeat the images
     }
    
    }
    
    </script>
    <script>
    
    myfunction1();  // element id slider  must exist before referencing it
    </script>
    </body>
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Nov 2013
    Posts
    44
    thanks a lot!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles