LearningCoder
12-12-2012, 12:45 AM
Hi all,
I have a script which generates a random image every 10 seconds. I have created an image object and set the attributes but when I try to display the image it is showing [object HTMLImageElement].
I searched around but can't seem to find an answer. Here is my code:
function change_image(){
var image = new Image();
var index = Math.floor((Math.random() * y));//generates a random number between 0-length of array
image.setAttribute("src",imgDir+"/"+image_names[index]);
image.setAttribute("alt",image_names[index]);
console.log(image);
/*var image = "<img src='"+imgDir+"/"+image_names[index]+"' alt='Gardenable Products' title='"+image_names[index]+"' id='random_image' />";*/
document.getElementById("image_div").innerHTML = image;
document.getElementById("text").innerHTML = product_text;
}
var image_names = new Array("bench1.jpg","bench2.jpg","bench3.jpg","bench4.jpg","bench5.jpg",
"binstore1.jpg","binstore2.jpg","binstore3.jpg","binstore4.jpg","binstore5.jpg",
"birdhouse1.jpg","birdhouse2.jpg","birdhouse3.jpg","birdhouse4.jpg","birdhouse5.jpg",
"gate1.jpg","gate2.jpg","gate3.jpg","gate4.jpg","gate5.jpg",
"pethousing1.jpg","pethousing2.jpg","pethousing3.jpg","pethousing4.jpg","pethousing5.jpg",
"planter1.jpg","planter2.jpg","planter3.jpg","planter4.jpg","planter5.jpg",
"shed1.jpg","shed2.jpg","shed3.jpg","shed4.jpg","shed5.jpg",
"table1.jpg","table2.jpg","table3.jpg","table4.jpg","table5.jpg");
var product_text = "<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature"
product_text += "from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked"
product_text += "up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage</p>";
var imgDir = "core/product_files";
window.onload = setInterval("change_image()",10000);
The line which is commented out inside the function works but I was advised to just change the attributes rather than creating a new element. Instead of innerHTML I have also tried appendChild, but when I use appendChild it doesn't show anything at all.
Thank you in advance for any help you can give me.
Kiind regards,
LC.
I have a script which generates a random image every 10 seconds. I have created an image object and set the attributes but when I try to display the image it is showing [object HTMLImageElement].
I searched around but can't seem to find an answer. Here is my code:
function change_image(){
var image = new Image();
var index = Math.floor((Math.random() * y));//generates a random number between 0-length of array
image.setAttribute("src",imgDir+"/"+image_names[index]);
image.setAttribute("alt",image_names[index]);
console.log(image);
/*var image = "<img src='"+imgDir+"/"+image_names[index]+"' alt='Gardenable Products' title='"+image_names[index]+"' id='random_image' />";*/
document.getElementById("image_div").innerHTML = image;
document.getElementById("text").innerHTML = product_text;
}
var image_names = new Array("bench1.jpg","bench2.jpg","bench3.jpg","bench4.jpg","bench5.jpg",
"binstore1.jpg","binstore2.jpg","binstore3.jpg","binstore4.jpg","binstore5.jpg",
"birdhouse1.jpg","birdhouse2.jpg","birdhouse3.jpg","birdhouse4.jpg","birdhouse5.jpg",
"gate1.jpg","gate2.jpg","gate3.jpg","gate4.jpg","gate5.jpg",
"pethousing1.jpg","pethousing2.jpg","pethousing3.jpg","pethousing4.jpg","pethousing5.jpg",
"planter1.jpg","planter2.jpg","planter3.jpg","planter4.jpg","planter5.jpg",
"shed1.jpg","shed2.jpg","shed3.jpg","shed4.jpg","shed5.jpg",
"table1.jpg","table2.jpg","table3.jpg","table4.jpg","table5.jpg");
var product_text = "<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature"
product_text += "from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked"
product_text += "up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage</p>";
var imgDir = "core/product_files";
window.onload = setInterval("change_image()",10000);
The line which is commented out inside the function works but I was advised to just change the attributes rather than creating a new element. Instead of innerHTML I have also tried appendChild, but when I use appendChild it doesn't show anything at all.
Thank you in advance for any help you can give me.
Kiind regards,
LC.