Create a DIV Tag using JavaScript

baasopchoppySecurity

Nov 5, 2013 (3 years and 7 months ago)

114 views

Create a DIV Tag using JavaScript

<
html
xmlns
="http://www.w3.org/1999/xhtml">


<
head
>


<
title
>
Create DIV Dynamically Using JavaScript
</
title
>


<
script
type
="text/javascript"
language
="javascript">



function
DynamicDiv() {


var
dynDiv =
document.createElement(
"div"
);


dynDiv.id =
"divDyna"
;


dynDiv.innerHTML =
"Created using JavaScript"
;


dynDiv.style.height =
"20px"
;


dynDiv.style.width =
"300px"
;


dynDiv.style.backgroundColor =
'gray'
;


doc
ument.body.appendChild(dynDiv);


}


</
script
>

</
head
>


<
body
>


<
div
>


<
input
id
="Button1"
type
="button"


value
="Using JS"
onclick
="DynamicDiv();" />



</
div
>

</
body
>


</
html
>

Create a DIV Tag using ASP.NET

<%
@
Page
Language
="C#"
AutoEventWireup
="true"
CodeFile
="Default5.aspx.cs"

Inherits
="Default5"
%>


<!
DOCTYPE
html PUBLIC
"
-
//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1
-
transitional.dtd">


<
html
xmlns
="http://www.w3.org/1999/xhtml">

<
head
id
="head
1"
runat
="server">

<
title
>
Create DIV Dynamically Using ASP.NET
</
title
>

</
head
>


<
body
>


<
form
id
="form1"
runat
="server">


<
div
>


<
asp
:
Button
ID
="btnCode"
runat
="server"


onclick
="btnCode_Click"


Text
="Code Behind" />


</
div
>


</
form
>

</
body
>


</
html
>


C#

protected void
btnCode_Click(
object
sender,
EventArgs
e)

{


System.Web.UI.HtmlControls.
HtmlGenericControl
dynDiv =


new
System.Web.UI.HtmlControls.
HtmlGenericControl
(
"DIV"
);


dynDiv.ID =
"dynDivCode"
;


dynDi
v.Style.Add(
HtmlTextWriterStyle
.BackgroundColor,
"Gray"
);


dynDiv.Style.Add(
HtmlTextWriterStyle
.Height,
"20px"
);


dynDiv.Style.Add(
HtmlTextWriterStyle
.Width,
"300px"
);


dynDiv.InnerHtml =
"I was created using Code Behind"
;


this
.Controls.Add(dynDiv);

}

VB.NET

Protected Sub
btnCode_Click(
ByVal
sender
As Object
,
ByVal
e
As
EventArgs)


Dim
dynDiv
As New
System.Web.UI.HtmlControls.HtmlGenericControl("DIV")


dynDiv.ID = "dynDivCode"


dynDiv.Style.Add(HtmlTextWriterStyle.Backg
roundColor, "Gray")


dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px")


dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px")


dynDiv.InnerHtml = "I was created using Code Behind"


Me
.Controls.Add(dynDiv)

End Sub

Similarly you can also check