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,177
  • 1
  • 27
  • 36

4 Answers4

2

Yes, you can use brackets for this:

myObject[ someVariable ] = 0;
antyrat
  • 26,950
  • 9
  • 73
  • 75
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,600
  • 2
  • 24
  • 34
1
$('.red_button')
    .each(function() {
        var someVariable = $(this).attr('name');
        myObject[someVariable] = 0;
    });
CD..
  • 68,981
  • 24
  • 147
  • 156
0

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

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

NimChimpsky
  • 44,999
  • 57
  • 192
  • 304