1

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/

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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;.
0

Try this -

var x = document.getElementById('pids').innerHTML;

Comments

0

http://jsfiddle.net/8vS2D/

var x = document.getElementById('pids');
var y = eval(x);

alert(eval(x.innerText));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.