Click to See Complete Forum and Search --> : loops, layers and imgs II
leechun
05-17-2003, 11:10 AM
hello everyone!
thanks for your input to my previous posts. after some thought, i think i need to rephrase my question a bit better. if i create layers and put a image in each layer like this:
for(index=0; index<8; index++)
{
.
.
.
document.write('<img id = "sec_img'+index+' ">');
or
document.write('<img id = "eval("sec_img" + index )" >');
.
.
.
}
doesn't this mean that the id of each image will be writen in the docmunet as sec_img0, sec_img1, sec_img2......?
if yes, i can't seem to be able to change aspects of the img by accessing them like sec_img0.src later, for example.
many thanks
CHUN
Just a note: Your second method with the eval() method wouldn't work because your quotes are in the wrong places. ;)
As for your question, you should be able to access the images using their respective IDs, but only if you run this particular code before your other JavaScript functions.
leechun
05-17-2003, 11:29 AM
thanks for point out the mistake:)
could you explain a bit on what you mean by running the code before "other" JavaScript functions. many thanks.
if you have got some time to spare and like to see my code in more detail, please go to www.sonicvariable.com and the "collbrative" page. idealy, i like to create the layers using for loop instead of writing the code repeatly. and in the code, there is the change_src2 function, which is where i wish to change the source of the image by referceing to the images as sec_img0, sec_img1.....
many thanks
CHUN
The change_src2() function is much different from your original "code" in your original post. You're randomly changing the src of the image? What reason would you have for this, may I ask?
khalidali63
05-17-2003, 11:34 AM
As I said before,you can avoid using document.write at every loop index,if you must use document.write use it in this manner
var htmlStr = "";
for(index=0; index<8; index++)
{
htmlStr +='<img id = "sec_img'+index+'.gif" alt="sec_img'+index+'.gif" ><br/>';
}
and once all of th eimages are done
then print themat once
document.write(htmlStr);
leechun
05-17-2003, 03:52 PM
hello:
well, the reason for changing the images at random is that i like the idea of having something that changes all the time, so that its not static. its generative, the way i call it. anyway, i was just experimenting with the possibilities of using JS to control aspects of web pages.
thanks for all your help
Personally I'd use a server-side language. For those of your visitors with JS disabled.