0

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?

3 Answers 3

1

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

2 Comments

actually activatables function doesnot work if I pass variable. It works only if I specify static values as .. activatables('section', ['Need For Guru','Qualities of a Guru',...]); ... any idea how can I solve this?
i am sorry.. it got solved.. it was due to '?' character the activatables() was not working... thx for ur help
0

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 );

Comments

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

1 Comment

when page loads, my code should have arr evaluated as ... activatables('section', ['Need For Guru','Qualities of a Guru',...]); ... then only this works

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.