Reputation: 1159
I am probably doing a very small and fundamental mistake here. I am getting some information in the dom which exactly looks like this
<span id="pids" style="display:none">["26551826","22956811","22959266"]</span>
Which then I am trying to convert into a js array. For that I am doing this
var x = document.getElementById('pids');
var y = eval(x);
alert(y.length);
And the result is undefined. What am I doing wrong here?
Here is my fiddle
http://jsfiddle.net/sghoush1/sbrmT/2/
Upvotes: 1
Views: 70
Reputation: 1
var x = document.getElementById('pids');
var y = eval(x);
alert(eval(x.innerText));
Upvotes: 0
Reputation: 148744
Try this : http://jsfiddle.net/sbrmT/3/
var x = document.getElementById('pids').innerText; //you need to get the value
var y = JSON.parse(x); //dont use eval , json.parse will do.
alert(y.length);
Upvotes: 2