0

Does anyone know haw to use a string to access a a javascript object? I need to use a var/string to access a javascript object.

++ This Question has been answered - See the JSFiddle for the fix ++

var answers = {"A":0,"B":0,"C":0,"D":0};

var abcd = $(this).attr("data-filter"); //this is equal to A
var answer_select = "answers."+abcd;    //this is equal to answers.A

answer_select=20; //does not change the value.
//if i write..
answers.A=20; //it works

//I need to use the variable "answer_select" as this changes each time..

I have setup a jsfiddle here..
http://jsfiddle.net/jstleger0/h3SEX/6/

0

1 Answer 1

2

Use this syntax: var answer_select = answers[abcd];

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

4 Comments

I have tried : var answer_select = "answers["+abcd+"]"; With no success, it's strange. Thank you for the response.
@jstleger0: No quotes! It's not a string literal you want to access or even build!
I have tried: var answer_select = answers.abcd; and: var answer_select = answers[abcd]; when a variable is used to construct the access to the object it doesn't work..
Hey just got this.. Thanks for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.