The solution is: Don't create code strings and put them in an array. Just execute the code:
$('#titel'+i).trumbowyg('html', data[i].value);
If those elements don't exist yet, just run this code later when they do, or if for some reason you won't have access to data
later, build an array of functions:
arrSetContentCommands.push(function(index, value) {
$('#titel'+index).trumbowyg('html', value);
}(i, data[i].value));
...and later:
arrSetContentCommands.forEach(function(command) {
command();
});
Notice in that we don't use i
or data[i]
in the function we're pushing; see this question's answers for why not.
If you had a good reason for executing code in a string, you'd do it via new Function(...)()
, like this:
arrSetContentCommands.forEach(function(command) {
new Function(command)();
});
But there are very, very, very few cases where that's appropriate, and obviously never do it with code you don't trust.