0

Possible Duplicate:
javascript object, access variable property name?

I'm sure it can be done but I could use some help...

$('.red_button')
    .each(function() {
        var someVariable = $(this).attr('name');
        myObject.someVariable = 0;
    });

Many thanks to my saviour!

Community
  • 1
  • 1
DevlshOne
  • 8,357
  • 1
  • 29
  • 37

4 Answers4

2

Yes, you can use brackets for this:

myObject[ someVariable ] = 0;
antyrat
  • 27,479
  • 9
  • 75
  • 76
1

What you are referring to is called dynamic object properties. In order to implement this functionality you would use the following syntax:

myObject[someVariable] = 0;
Robert
  • 8,717
  • 2
  • 27
  • 34
1
$('.red_button')
    .each(function() {
        var someVariable = $(this).attr('name');
        myObject[someVariable] = 0;
    });
CD..
  • 72,281
  • 25
  • 154
  • 163
0

that is correct, assuming you have created the myObject before using it :

http://jsfiddle.net/73WZ3/1/

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311