0

this is probably ver simple but I cannot figure it out.

how can I assign the objects values to the buttons style atribute in the for loop below?

(function(){
    var parent = {
        color: "orange",
        width: "50px",
        height: "50px"
    }, 
        child = Object.create(parent),
        button = document.querySelector("button");

    button.onclick = function(){
        for (var test in child) {
            button.style.test = // ??
        }
    }

})();

thanks in advance

1 Answer 1

4

For such cases there is a square bracket notation:

button.style[test] = child[test];

MORE: http://www.javascripttoolbox.com/bestpractices/#squarebracket

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

5 Comments

Just beat me to it. ;)
This is one of those "if only it was jQuery" questions, button.css(parent) would do!
I was just wondering why hasOwnProperty isn't needed in this example. Is it because of Object.create?
@Ejay If you're certain of what your objects contain, you rarely need that.
this is exactly what i needed .. thanks for the fast response!

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.