0

Consider a simple JS function as

function test(property, color) {
    var element = document.getElementById("test");
    element.style.PROPERTY = color; // I want to use a variable for this command
}

In a typical JS command of element.style.PROPERTY, I want to introduce the property as a variable. This is a very basic example; what is the best approach to use variables in JS command lines?

1

2 Answers 2

3

Use it as:

element.style[property]

This is a very basic example; what is the best approach to use variables in JS command lines?

It depends on what you want to achieve. In this case - just use [...]

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

1 Comment

And, of course, .replace(/-(.)/g, function(a, b) { return b.toUpperCase(); }) for all those quirky hyphenated properties! And then, cssFloat. And then, -ms prefixes.
1

element.style[property] = color; should work.

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.