hetal gala
hetal gala

Reputation: 249

variable value to use as javascript code

I have following code...

<script type="text/javascript">

var arr= new Array('10');


arr[0]='Need For Guru';
arr[1]='Qualities of a Guru';
arr[2]='Living Guru';
arr[3]='Who is a Satguru?';
arr[4]='Guru and Spiritual master';
arr[5]='Definition of discipleship';
arr[6]='Power of Faith';
arr[7]='Bad mouthing a Guru';
arr[8]='Fake Guru Shishya';
arr[9]='Pitfalls in the path of liberation';


var text='['+arr[0]+',\''+arr[1]+'\','+arr[2]+',\''+arr[3]+'\','+arr[4]+',\''+arr[5]+'\','+arr[6]+',\''+arr[7]+'\','+arr[8]+',\''+arr[9]+'\']';
document.write(text);

activatables('section', text );

</script>

My problem is that I want to use value of text as part of javascript code in line --- activatables('section', text ); .. so that code becomes like below

    activatables('section', ['Need For Guru','Qualities of a Guru',...]);

But I am not able to do the same.. .can anyone help in this?

Upvotes: 0

Views: 69

Answers (3)

Stuart Wakefield
Stuart Wakefield

Reputation: 6414

I think I understand what you are getting at... Do you want to serialize the array as text, and deserialize text into a new array?

To turn it into a string...

var arr = ["Need for a Guru", "Qualities of a Guru" /*, etc... */ ];
var text = JSON.stringify(arr);

To turn the string into an array...

var text = '["Need for a Guru", "Qualities of a Guru"]';
var arr = JSON.parse(text);
activatables('section', arr);

Obviously you don't need to deserialize / parse the text if you already have access to the source variable... You can just plug it straight in.

var arr = ["Need for a Guru", "Qualities of a Guru" /*, etc... */ ];
activatables('section', arr);

Upvotes: 1

deceze
deceze

Reputation: 522110

activatables('section', ['Need For Guru','Qualities of a Guru',...]);

means you're passing an array to activatables. With your current code you get that effect by passing arr, which is the array you want:

activatables('section', arr);

Upvotes: 0

user1726343
user1726343

Reputation:

Why are you doing this? If you want a string representation of your array, use:

JSON.stringify(arr);

Which can be passed as an argument using:

activatables('section', JSON.stringify(arr) );

However, in your case it seems you want to pass the string section and the array itself as arguments to the function activatables. If this is the case, all you need to do is:

activatables('section', arr );

Upvotes: 0

Related Questions