469,890 Members | 2,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

incrementing variable names (not sure how to describe it)

hi,

i am trying to use a function that will write html into empty <span>
tags. the <span> tags will all be numbered incrementally and written
to using the same function each time. below is my javascript at the
top of the page in the <head> part:
<head>
<script>
<!--

var global_num

function fstarter () {

global_num = 1; //initiate variable as 1

}

function fwriteinside () {

getElementById('item' & global_num).innerHTML = '<p>HTML Code,
yeah!</p>';

global_num = global_num + 1; //increment var

}

-->
</script>
</body>

Below is how the empty <span> tags will be writen:

<body onLoad="fstarter()">
<span onClick="fwriteinside();" style="cursor: hand;">Add HTML</span>
<span id="item1"></span>
<span id="item2"></span>
<span id="item3"></span>
</body>

When the function, fwriteinside, is called it will write to the next
span. this way the function is alot more efficient and expandable (can
keep adding new <span> tag slots). unfortunately it isnt working as im
not sure how you would correctly do this. any help would be great.
cheers

burnsy
Jul 23 '05 #1
1 2006
Lee
mr_burns said:

hi,

i am trying to use a function that will write html into empty <span>
tags. the <span> tags will all be numbered incrementally and written
to using the same function each time. below is my javascript at the
top of the page in the <head> part:
<head>
<script>
<!--

var global_num

function fstarter () {

global_num = 1; //initiate variable as 1

}

function fwriteinside () {

getElementById('item' & global_num).innerHTML = '<p>HTML Code,
yeah!</p>';

global_num = global_num + 1; //increment var

}

-->
</script>
</body>

Below is how the empty <span> tags will be writen:

<body onLoad="fstarter()">
<span onClick="fwriteinside();" style="cursor: hand;">Add HTML</span>
<span id="item1"></span>
<span id="item2"></span>
<span id="item3"></span>
</body>

When the function, fwriteinside, is called it will write to the next
span. this way the function is alot more efficient and expandable (can
keep adding new <span> tag slots). unfortunately it isnt working as im
not sure how you would correctly do this. any help would be great.
cheers


Your primary problem is in the expression:
'item' & global_num

In Javascript, as in many other computer languages, the '&'
symbol is the bit-wise AND operator. Javascript overloads
the '+' operator to concatenate strings:
'item' + global_num

There are several other things that I would do differently,
including dropping the obsolete "<!--" and "-->" lines,
specifying the type="text/javascript" attribute of the
<script> tag, using the increment operator (global_num++),
initializing global_num in-line, instead of in the onLoad
handler, and beginning every sentence in your post with a
capital letter.

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

83 posts views Thread by Alexander Zatvornitskiy | last post: by
3 posts views Thread by Mothra | last post: by
18 posts views Thread by Patrick Wood | last post: by
3 posts views Thread by Charles E. Vopicka | last post: by
27 posts views Thread by Erik de Castro Lopo | last post: by
53 posts views Thread by subramanian100in | last post: by
112 posts views Thread by istillshine | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.