0

My problem is driving me nuts.

I have declared som variables like this

var tip01 = "This is my first tip";
var tip02 = "This is my second tip";

Depending on the value in my querystring I want to write out my tips.

my querystring: ex ?myQSvalue=01

var myQSvalue = getQueryString('myQSvalue');
var myTip = 'tip' + myQSvalue;

 $("#output").html(myTip);

But the output is: tip01 insted of This is my first tip.

How can I achieve this and output the content in my declared varable?

Thank you.

0

4 Answers 4

4

rather than different variabes, you should be using an array

var myTips = [];
myTips[0] = "This is my first tip";
myTips[1] = "This is my second tip";

Then pass an index on the querystring ?myQSvalue=0

And read from the array:

var myQSvalue = parseInt(getQueryString('myQSvalue'),10);
$("#output").html(myTips[myQSvalue]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i went with the array approach.
0

I'd suggest defining the tips in an array instead, but you should be able to use:

$("#output").html(window[myTip]);

Comments

0

The correct approach has already been pointed out by Jamiec, but just for future reference, you can refer to global variables as properties of the global context:

var myQSvalue = getQueryString('myQSvalue');
var myTip = 'tip' + myQSvalue;

 $("#output").html(window[myTip]);

Comments

0

Thanks for all your help. I went with the array approach and it works great.

But the $("#output").html(window[myTip]); didn´t work for me.

In some cases that approach would be handy.

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.