1

I have a variable which contains IDs.

var mediaid = '5';

And i have a variable set

var t1 = 'First';
var t2 = 'Second';
var t3 = 'THird';
etc...

I'm trying to get variable's variable inside of jQuery's .append function.

$('#block').append('<span>{t+mediaid}</span>');

For example if mediaid is 3, {t+mediaid} should be t3. But i have syntax errors. Can you fix it..

7
  • @Quentine normally i will use an array but this time there is a detail, so i can't use array. I'm not writing this detail because it's not related with my question :) But thank you you're right :) Commented May 26, 2011 at 18:25
  • @Eray Although that detail is not related to this question, I would still like to hear about it. Could you please elaborate why you need that variable set (in short)? Commented May 26, 2011 at 18:30
  • @Sime, this set (t1, t2, t3...) contains long texts and i need to find (for example) 5th element quickly when i need. If i use array (t['lorem','ipsum','lorem'....]) i can't find 11th element easily Commented May 26, 2011 at 18:34
  • @Eray, yes you can.... 11th element = t[11-1] Commented May 26, 2011 at 18:36
  • @Eray You can place each string in its own line. See here. Commented May 26, 2011 at 18:41

3 Answers 3

3
$('#block').append('<span>'+{t+mediaid}+'</span>');

I dont think that this is possible.

You might have to do :

$('#block').append('<span>'+window['t'+mediaid]+'</span>');
                           //if all those variables are in the window's scope

Better:

var mediaid = '5';
var t = ['', 'first', 'second', 'third', ...];

$('#block').append('<span>'+t[mediaid]+'</span>');
Sign up to request clarification or add additional context in comments.

4 Comments

No, that will return the value of t (undefined) and mediaid rather than the variable he wants.
@Jeremy, that is what is there
@Neal The null value usually represents an empty object reference. Since the other values are strings, an empty string ('') would be more appropriate I believe...
@sime, that is true.. changed it
1

Why not store your variables in an array instead of magically-named variables? Then you can access array elements by index.

var mediaid = 5;
var t = [
    'Zeroth',
    'First',
    'Second'
    // etc...
];

$('#block').append('<span>' + t[mediaid] + '</span>');

Comments

-1

Try this:

$('#block').append('<span>' + eval('t'+mediaid) + '</span>');

2 Comments

you is not need of 'eval()' for this, access the variable by 'window ' object $('#block').append(['<span>' , window[t + mediaid], '</span>'].join(''));
@The Mask: if your document will contain <div id=t5></div> then window["t5"] will return you reference to that DOM element, not value of variable t5. See: stackoverflow.com/questions/6130340/…

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.