👍:0
Basically the answer is that document.getElementById("json")
just gives you an HTML element. If you want the contents of that element, use document.getElementById("json").innerHTML
.
However, the contents of innerHTML
will be a string, not a Json object. If you want the contents of myData
to be a Json object rather than just text formatted to look like Json, you would need to use JSON.parse()
on that, or just do something more like:
var myData = <?php echo $json; ?>;
OR
var myData = [ <?php echo $json; ?> ];
Not sure what the array part is for, but maybe you need it.
👍:0
It looks like you’re pulling in the DOM element into an array?
Have you tried: data = JSON.parse(document.getElementById(“json”).innerHTML)
That should pull in the JSON string, and then parse it if it was properly stringified (and not an object, else you’ll get an error).