soum
soum

Reputation: 1159

converting a string into a js array

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

Answers (3)

rsvpilot
rsvpilot

Reputation: 1

http://jsfiddle.net/8vS2D/

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

alert(eval(x.innerText));

Upvotes: 0

Kamehameha
Kamehameha

Reputation: 5488

Try this -

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

Upvotes: 0

Royi Namir
Royi Namir

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

Related Questions