var name = new Array('freddy', 'mac', 'fanny', 'may');
var content = 'hi '.+name[i]+.'!';
$('div.class').each(function (i) {
this.html(content);
});
This javascript should output
hi freddy!
hi mac!
hi fanny!
hi may!
but there is an error for the var because it refrences an array that has not been set up yet.
You are assigning a value to the
contentvariable, and a value is evaluated when you assign the value, not when you use the variable. I.e. a plain value can not contain a reference to another value.You can make it a function that evaluates the value when it’s called: