I have created a script that contains the following code:
var position = 1;
var backButton = document.getElementById("theBackButton");
var nextButton = document.getElementById("theNextButton");
var introPos = document.getElementById("introductionText");
var posOne = document.getElementById("positionOne");
var posTwo = document.getElementById("positionTwo");
var posThree = document.getElementById("positionThree");
var posFour = document.getElementById("positionFour");
var posFive = document.getElementById("positionFive");
var posSix = document.getElementById("positionSix");
var posSeven = document.getElementById("positionSeven");
var posEight = document.getElementById("positionEight");
var posNine = document.getElementById("positionNine");
var posTen = document.getElementById("positionTen");
var posEleven = document.getElementById("positionEleven");
Each position contains different content and I use buttons to move between each position. The script works just fine, but I am wondering if there is a more efficient way to generate these variables and assign them to these values? I have to do this type of work a lot, and in my attempt at learning JavaScript I read something to the effect of "If you find yourself having to write the same type of code over and over again, you're probably not doing it right."
I know ahead of time how many positions that I will have and I'm thinking that I could use a loop to do this, but I'm not sure. The results I hope to get is a bunch of variables [posOne, posTwo, posThree, etc] with the corresponding value of document.getElementById("positionOne");
Thanks.