Nere
Nere

Reputation: 4097

Object in class set as variable

Here is my javascript:

var error_msg = error_data.validation_msg;
if(error_msg)
{
     for(var i=0; i<5; i++)
     {
          var answer_exact = 'answer_exact_'+i;
          var error = error_msg.answer_exact;
     }         
}

How I can call objects in error_msg class like:

error_msg.answer_exact_0;
error_msg.answer_exact_1;
error_msg.answer_exact_2;
....

Upvotes: 0

Views: 29

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use bracket notation to get a property using a dynamic key(where the property name is stored in a variable).

var error = error_msg[answer_exact];

Your code tries to read a property named answer_exact from the object error_msg

Upvotes: 3

Related Questions