Question

I'm a beginner in JavaScript and need some quick help. When "for loop" begins, I want data1 to change to data2, data3 and so on. I've tried many expressions like ("data[i]") and so on, but nothing happens. Can you help me?

for (var i=1;i<10;i++){
                document.getElementById("data1").innerHTML = "text";
            }
Était-ce utile?

La solution

Just use string concatenation.

for (var i=1;i<10;i++){
     document.getElementById("data" + i).innerHTML = "text";
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top