i++ and showing the value on innerHTML

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

i++ and showing the value on innerHTML

Post by lovelf »

Hello, I have the code below which writes the values but how to assign them to an id with innerHTML?

Code: Select all

<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
I tried:

Code: Select all

<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
document.getElementById("anyId").innerHTML = i + "<br>";
}
</script>
<span id="anyId"></span>
</body>
</html>
But it did not work :?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: i++ and showing the value on innerHTML

Post by papa »

Code: Select all

 
<span id='anyId'></span>
 
<script type="text/javascript">
var i_list = '';
for (var i=0;i<=10;i++)
 {
     i_list += 'The number is: ' + i + '<br />';
 }
document.getElementById("anyId").innerHTML = i_list;
</script>
 
Something like that ?
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: i++ and showing the value on innerHTML

Post by lovelf »

Yes. Thanks papa.

Now, I am trying to store variables depending on the value of i, x times. x amount of variables.

Code: Select all

 
<span id='istart'></span>
<span id='istartb'></span>
<script type="text/javascript">
var i_list = '';
var cac = '5';
var pich = '.png';
for (var i=0;i<=cac;i++)
 {
i_listb = i + '' + pich +'';
     i_list += i + '' + pich +'';
}
var lala = i_list;
var lalab = i_listb;
document.getElementById("istart").innerHTML = lala;
document.getElementById("istartb").innerHTML = lalab;
</script>
 
I am trying to do a slide show in which I can set the number of images there will be displayed in variable cac.

I would want to store x amount of variables depending cac's value.

Code: Select all

i_listb = i + '' + pich +'';
gives me all numbers sequentially.

while code:

Code: Select all

i_listb = i + '' + pich +'';
outputs the last number.

Now, how could I get each number as a var?

var1, var2, and so on.
Post Reply