Reputation: 29
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.
Upvotes: 0
Views: 289
Reputation: 29
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.
Upvotes: 0
Reputation:
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]);
Upvotes: 0
Reputation: 191749
I'd suggest defining the tips in an array instead, but you should be able to use:
$("#output").html(window[myTip]);
Upvotes: 0
Reputation: 136104
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]);
Upvotes: 4