22

I have an object

var Messages = {
'fullName' : 'Tell us your cool name dude..! e.g. Yousuf Iqbal',
'userName' : 'Choose a catchy username. Remember! It should be available :)',
'password' : 'Choose a top secret password with special chars, numbers and alphabets',
'rePassword' : 'Retype the password you just typed. But, don\'t try to copy!',
'bYear' : 'Tell the year, in which this bomb blasted'
};

and a variable..

var attribute = $('#userinfo form input').attr('name');

now i want to select Messages object property using this variable like this..

var message = Messages.attribute;

but Its not working.. and have also tried the following..

var message = Messages+'.'+attribute;
Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57

2 Answers2

49

Square brackets:

message = Messages[ attribute ];
Pointy
  • 405,095
  • 59
  • 585
  • 614
8
var message = Messages[attribute];
Lior Cohen
  • 8,985
  • 2
  • 29
  • 27