views:

46

answers:

3

Here's the quick version of the code as it stands right now:

function foo(attributeName, someJSObj, key, newValue)
{
    someJSObj[key].attributeName = newValue;
}

Obviously this doesn't work, since it just creates a new element called attributeName. Is there an easy way to dereference the attributeName into the string that represents some existing attribute on someJSObj?

+5  A: 

You need to use the bracket notation for attributeName as well:

function foo(attributeName, someJSObj, key, newValue)
{
    someJSObj[key][attributeName] = newValue;
}

Now the value of attributeName is used as identifier instead of the identifier attributeName itself.

Gumbo
This works! Didn't know that properties could be referenced ala second order arrays.
somerandomguy
A: 

Try someJSObj[key].setAttribute(attributeName, newValue)

muddybruin
+1  A: 

If I understood you correctly, you could use ECMAScript

function foo(attributeName, someJSObj, key, newValue)
{
    someJSObj[key][attributeName] = newValue;
}

Hope this helps you.

The Elite Gentleman