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
asked Mar 16, 2014 at 20:24
soum
1,1593 gold badges21 silver badges49 bronze badges
3 Answers 3
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);
answered Mar 16, 2014 at 20:26
Royi Namir
149k145 gold badges504 silver badges833 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
RobG
innerText is an IE property copied by some, but not all, browsers. The standards–compliant equivalent is textContent. But since old IE doesn't support textContent, do something like
var el = document.getElementById('pids'); var x = el.textContent || el.innerText;.Try this -
var x = document.getElementById('pids').innerHTML;
answered Mar 16, 2014 at 20:26
Kamehameha
5,4781 gold badge25 silver badges31 bronze badges
Comments
var x = document.getElementById('pids');
var y = eval(x);
alert(eval(x.innerText));
Comments
lang-js