I'm trying to write a loop to improve the way I assign values to html elements on a web page.
at the moment I use....
document.getElementById("Season201516Score1").innerHTML = Game201516Score1;
document.getElementById("Season201516Score2").innerHTML = Game201516Score2;
document.getElementById("Season201516Score3").innerHTML = Game201516Score3;
and so on....
The variables are
var Game201516Score1= "L 0-2"
var Game201516Score2= "W 1-0"
var Game201516Score3= "D 2-2"
The loop i'm using is
for (var i = 1; i <= 3; ++i)
{
element = 'Season201516Score' + i
Score = ('Game201516Score' + i)
document.getElementById(element).innerText = Score;
}
It works in so far that it puts text in the correct elements on the page but rather than the values "L 0-2
", "W 1-0
" or "D 2-2
" it gives the text "Game201516Score1
", "Game201516Score2
" and "Game201516Score3
".
Can someone show me what I'm doing wrong.