0

I am creating an application where I need to style a div as follows using javascript;

var div = document.createElement("div");
div.innerHTML = "xxxxxxxxxx";
div.style.display = "block";
div.style.width = "100%";
div.style.height = "10%";
div.style.background = "#f2dede";
div.style.color = "black";
div.style.position = "fixed";
div.style.padding = "20px";
div.style.top = "50%";
div.style.border = "5px solid #ebccd1";
//div.style.border-radius = "20px";
//div.style.text-align = "center";
//div.style.webkit-border-radius = "20px 0 0 20px";
//div.style.moz-border-radius = "20px 0 0 20px";
document.body.appendChild(div);

There's a fiddle here.

The problem I am facing is that none of the commented lines above will work. As soon as I uncomment them the div won't show.

What could be the cause of this?

6 Answers 6

4

You Need to change the code to

div.style.borderRadius = "20px";
div.style.textAlign = "center";
div.style.webkitBorderRadius = "20px 0 0 20px";
div.style.mozBorderRadius = "20px 0 0 20px";

Name of the properties are not same in the javascript as they are in css.

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

Comments

2

In javascript, an object member can't contain - in its name. You should replace this:

//div.style.border-radius = "20px";
//div.style.text-align = "center";
//div.style.webkit-border-radius = "20px 0 0 20px";
//div.style.moz-border-radius = "20px 0 0 20px";

with

div.style.borderRadius = "20px";
div.style.textAlign = "center";
div.style.webkitBorderRadius = "20px 0 0 20px";
div.style.mozBorderRadius = "20px 0 0 20px";

Comments

2

You need to change the casing of those properties to camelCase. '-' is an invalid character in JS variable names.

div.style.borderRadius = "20px";
div.style.textAlign = "center";    

Comments

2

Name of the CSS properties are different in JS.

div.style.borderRadius = "20px";
div.style.textAlign = "center";
div.style.webkitBorderRadius = "20px 0 0 20px";
div.style.mozBorderRadius = "20px 0 0 20px";

Updated Fiddle

HTML Element Style

Comments

1

Used to this CamelCase as like this

div.style.borderRadius = "20px";
div.style.textAlign = "center";

Comments

1

Working Link .Your code will be :

var div = document.createElement("div");
div.innerHTML = "xxxxxxxxxx";
div.style.display = "block";
div.style.width = "100%";
div.style.height = "10%";
div.style.background = "#f2dede";
div.style.color = "black";
div.style.position = "fixed";
div.style.padding = "20px";
div.style.top = "50%";
div.style.border = "5px solid #ebccd1";
div.style.borderRadius  = "20px";
div.style.textAlign = "center";
div.style.webkitBorderRadius = "20px 0 0 20px";
div.style.mozBorderRadius = "20px 0 0 20px";
document.body.appendChild(div);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.