/    Sign up×
Community /Pin to ProfileBookmark

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>
[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsNov 19.2013 — [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>[/CODE]
Copy linkTweet thisAlerts:
@alzamiauthorNov 20.2013 — thanks a lot!!
×

Success!

Help @alzami spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...