0

Wanting to run some tests, how can I achieve the following:

function(){console.log('console me')} within the data surrounded by text, but I don't want the data displaying in HTML as it currently does, I want it to execute instead, and only for the surrounding text to display on the page.

JSON Data

[{ "id": "1", "text": "I am testing console log from within json data function(){console.log('console this text')} so the text before this should not appear in html, rather it should run and all this other text should appear"}] 
Dave
  • 691
  • 2
  • 11
  • 25
  • 1
    The JSON isn't the right place to be looking. It's whatever you're using to put things on the page that you should be looking at using to add a function. You can't run functions just by putting them in random string values in a JSON structure. You'd have to have whatever's *consuming* the JSON extract that code snippet and make it get evaluated (it might do that by putting that text within a `script` tag, or calling `eval` on it). You need to step back and look at a different approach altogether. – T.J. Crowder Aug 25 '13 at 09:44
  • You have to build a directive for example because functions are not allowed in JSON. http://stackoverflow.com/a/2001471/1165289 – Bernhard Aug 25 '13 at 09:44
  • Ahhh I get exactly what you mean :) How would I build a directive for it though? Just want to test this :) thanks!! – Dave Aug 25 '13 at 09:46
  • Does this answer your question? [Is it valid to define functions in JSON results?](https://stackoverflow.com/questions/2001449/is-it-valid-to-define-functions-in-json-results) – Sarabadu Dec 11 '20 at 21:34

0 Answers0