0

I am using javascript. I have a global variable like:

var contentlabel = {
'post':'Posted Blogpost',
'like':'Liked your Blogpost'
};

I want to access the value of contentlable.post dynamically in another function dynamically based on the value retrieved (like, post, ..) as below:

var CONTENT_TYPE=content[i].contentType;
alert(CONTENT_TYPE);   //line 1  --> proper output
alert(contentlabel.CONTENT_TYPE);  //line 2  --> undefined
alert(contentlable.post);  //line 3  --> proper output

Where content is an array consisting of 'post' and 'like'. At line 1 I am getting proper output say: post or like In place of 'post' I want to display 'Posted a Blog', so I tried like contentlabel.CONTENT_TYPE, but its not replacing CONTENT_TYPE with either post or like.

Can anyone suggest a way to bind this dynamically.

jsFiddle: http://jsfiddle.net/HWRwt/

1 Answer 1

5

You should use square bracket notation: contentlabel[CONTENT_TYPE].

MORE: http://www.jibbering.com/faq/faq_notes/square_brackets.html

Sign up to request clarification or add additional context in comments.

1 Comment

@bfavaretto lmao :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.