Lukas Baliƫnas Lukas Baliƫnas - 1 year ago 108
HTML Question

Naming variables in for loop

I'm new to JS and HTML. I learned that it's possible to name a variable by code. So i tried some simple variable naming code. However, it doesn't print anything on the page.

What seems to be the problem?


for (var i = 0; i < 10; i++) {
window['var' + i] = i;
document.getElementById("text").innerHTML = "" + var8;


<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<script src="JScript.js"></script>
<p><span id="text"></span>left</p>

Answer Source

Enclose your javascript code in below document.ready function, so that it will execute only when your page is ready.

$(document).ready(function () {
    // enter your code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download