Click here to Skip to main content
15,915,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I dynamically create a div in my page.
The code is below:-
-------
function CreateDiv()
{
var codeDiv = document.createElement('div');
codeDiv.id = 'codeDiv';
codeDiv.innerHTML = "Created using JavaScript";
codeDiv.style.position = "relative";
codeDiv.style.borderStyle = "solid";
codeDiv.style.borderWidth = "1px";
codeDiv.style.borderColor = "Red";
codeDiv.style.height = "250px";
codeDiv.style.width = "400px";
codeDiv.style.left = "150px";
codeDiv.style.top = "50px";
codeDiv.style.backgroundColor = 'gray';
document.body.appendChild(codeDiv);
}

Above code is created fine a div on we page.
But i want dynamically to add tag & accessing the different style+position+color.
How will i do to solve above problem???

Please help me anybody.........

How will
Posted

1 solution

Use Style.Add(<css properties here>, <css value here>).
you can add as many css properties as you like.

Examples:
C#
codeDiv.Style.Add("padding", "5px 1px 2px 5px");
codeDiv.Style.Add("border-style", "solid none solid none");
codeDiv.Style.Add("border-color", "Red Yellow Blue Green");
codeDiv.Style.Add("border-width", "1px 2px 3px 4px");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900