Access Javascript variables dynamically

If they are variables in the window scope, then you can access window['option'+i]. However, you really should just use an array:

var option = [
    "some text",
    "option 2",
    "option 3"
];
for( var i=0; i<3; i++) alert(option[i]);

Leave a Comment