1

In javascript I create a certain command:

arrSetContentCommands.push( "$('#titel"+i+"').trumbowyg('html', \""+data[i].value+"\");";`) 

This enables me to use a html editor in browser. The id's it needs to use are also created, cause it depends on what's in the database. how do i execute this? all the commands are placed in an array.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Achiel Volckaert
  • 1,004
  • 11
  • 28
  • 1
    Why you are you building code strings in an array rather than just executing the code? – T.J. Crowder Feb 22 '18 at 11:19
  • Im with @T.J.Crowder on this one, but you could use `eval()`, i would not recommend it tho.. – keja Feb 22 '18 at 11:21
  • I build these in a loop, depending on the result of the DB, just don't know how to execute – Achiel Volckaert Feb 22 '18 at 11:22
  • 1
    @AchielVolckaert: That doesn't answer the question. – T.J. Crowder Feb 22 '18 at 11:22
  • Why I do this? I want this reusable, so that I can use it with only a DB edit and minimal code edits – Achiel Volckaert Feb 22 '18 at 11:24
  • 1
    @AchielVolckaert: No, why are you building code strings rather than just executing the code? There's nothing in the question suggesting any reason you can't just make these calls in the loop, based on the results from the database. – T.J. Crowder Feb 22 '18 at 11:24
  • 1
    This sounds like an [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). If you tell us what you're really trying to do, we can probably help you do it in a better way than with code strings. – T.J. Crowder Feb 22 '18 at 11:30
  • @T.J.Crowder I'm creating a CMS, where I get my data with an ajax call to a php script. currently, i loop this, in the loop, there is a switch case that sets creates the element also in an array. which I then set as innerhtml pasted my current JS in jsfiddle. https://jsfiddle.net/kp7g1908/ – Achiel Volckaert Feb 22 '18 at 11:36
  • @AchielVolckaert: Any code relevant to the question needs to be **in** the question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to go off-site to help you. I'm not seeing any reason in that comment that you wouldn't have access to the elements during that loop. – T.J. Crowder Feb 22 '18 at 11:46

1 Answers1

2

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I can't just execute the code since the elements they need to access are not yet created at that point. – Achiel Volckaert Feb 22 '18 at 11:25
  • @AchielVolckaert: Then just wait until they are, or put functions in your array. Building code in strings is *(almost)* never the answer. – T.J. Crowder Feb 22 '18 at 11:26
  • @AchielVolckaert: I've added an example of putting functions in the array, but really, I'd just keep `data` and then do the loop when the elements exist. – T.J. Crowder Feb 22 '18 at 11:29