Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I access a dynamically created span id's innerText?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Here's the code:

for (N=1; N<=10; N++)
{
<span id=N></span>
}

This will create 10 span fields with id's of 1 to 10

How do I access each of these id's innerText without hardcoding them?

for (N=1; N<=10; N++)
{
N.innerText = N
}

This doesn't work, as it's trying to write to N.innerText and not 1.innerText to 10.innerText? I need to access N's variable & not N itself!

Thanks if you can help

Neil Kightley
 
in IE, this is easy. First of all, remember that valid ids cannot start with a number, so change your span generating code to:

for (N=1; N<=10; N++)
{
document.body.innerHTML+=&quot;<span id='span_&quot;+N+&quot;'></span>&quot;
}

Then to access them:

for (N=1; N<=10; N++)
{
document.getElementById(&quot;span_&quot;+N).innerText = N;
} jared@eae.net -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top