Closed Thread Icon

Topic awaiting preservation: Help needed (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8186" title="Pages that link to Topic awaiting preservation: Help needed (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Help needed <span class="small">(Page 1 of 1)</span>\

 
Lala
Bipolar (III) Inmate

From: Montreal, Canada
Insane since: Feb 2002

posted posted 05-26-2002 16:21

im trying to use a loop to change a bunch of images at once, the name of the pictures r 13p.jpg 14p.jpg and so on, the thing is i just cant figure out the rite sintax (as usual)

function cambia2()
{
for(i=13; i <= 24; i++)
{
document.getElementById(i).innerHTML =<img src='+i+"p.jpg"'>;
}
}

100 pics
Lala

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-26-2002 17:30

Ah, ok, You've almost got it. If this is hard for you, then let me give you some advice on how to go about it.

This line is the problem:
document.getElementById(i).innerHTML =<img src='+i+"p.jpg"'>;

Let's say you wanted to make them all 13p.jpg. Just to make things easier. To do that, you would use this code:

document.getElementById(i).innerHTML = '<img src="13p.jpg">';

Notice that quotes are required around the entire string that you want to assign to innerHTML. Single quotes or double quotes, your choice, but I usually use single quotes for the JavaScript since the HTML inside the string is required to have double quotes. (If you used *double* quotes to surround the string, then you would have to precede the inside double quotes with backslashes. But we won't get into that right now.)

OK. Now it's important to realize that those double quotes within '<img src="13p.jpg">' have absolutely nothing to do with the single quotes surrounding the whole thing. That distinction is important when we take the variable i into the picture:

document.getElementById(i).innerHTML ='<img src="' + i + '.jpg">';

I just cut the string into two halves, and then concatenated them with the value of the variable i inbetween. What you see there is a combination of the following three things:

<imc src="
the value of i
.jpg">

The first of the three things is a string - you want that actual text to be part of innerHTML. So you have to put it in quotes.
The second of the three things is the value of a variable. You can just put the variable there and it will automatically insert its value into the string.
The third of the three things is another string which requires quotes around it.

All three must be joined with a + sign, as follows:
'<img src="'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.jpg">'

So that should work.

There is one more thing required for it to work: the elements whose innerHTML's you're changing must have IDs set to "13", "14", "15", etc. That's because of this part:

document.getElementById(i)

The variable i has the values 13, 14, 15, etc, so the code is going to try to change the elements with those IDs. If your elements have IDs more like "image13", "image14", etc, or "num13", "num14", etc, or anything else with more than just the number, then you'll have to replace the i in document.getElementById(i) with a string that will match the IDs. For example, document.getElementById('image' + i).

Hope that helps.

[This message has been edited by Slime (edited 05-26-2002).]

Lala
Bipolar (III) Inmate

From: Montreal, Canada
Insane since: Feb 2002

posted posted 05-26-2002 17:46

Yup yup it helps...thanks a lot, i knew the id part , you were very clear and helpfull thanks again.

100 slimes

Lala

« BackwardsOnwards »

Show Forum Drop Down Menu