incrementing variable names (not sure how to describe it)

M

mr_burns

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
 
L

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,260
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top