0

I tried to create div using javascript, but struck up at one point. Here is the code.

<script type="text/javascript">

    var mydiv = document.createElement("div");
    mydiv.id = "div1";
    mydiv.style.height = "200px";
    mydiv.style.width = "200px";
    mydiv.style.background-color = "red";

    document.body.appendChild(mydiv);

Your help is highly obliged.

Thanks in advance

1

2 Answers 2

4

background-color is an invalid identifier in JavaScript. You can use backgroundColor instead.

mydiv.style.backgroundColor = "red";

Any CSS property with a dash in it is done in camelCase on the style object. So backgroundColor, marginLeft, etc. The values you assign are strings, so this doesn't apply to them, just the property names.

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

Comments

0

It should be mydiv.style.backgroundColor

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.