0

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?

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This works! Didn't know that properties could be referenced ala second order arrays.
1

If I understood you correctly, you could use ECMAScript

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

Hope this helps you.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.