Solved

using Javascript to render a div tag

Posted on 2016-07-14
3
33 Views
Last Modified: 2016-07-15
I have a html page.

Is there a way to use JavaScript to render a div that looks like this in the body section of my page. On page load.

<body>
        <div id="Div1">
            <p>Hello</p>
        </div>
</body>
0
Comment
Question by:maqskywalker
3 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 250 total points
ID: 41711247
try:
<script type="text/javascript">
window.onload=function(){
   document.getElementById("Div1").innerHTML = "<p>Hello</p>";
};</script>

Open in new window

0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 250 total points
ID: 41711328
The above method would replace the entire contents of the DIV. The DOM way would be like this:
<script type="text/javascript">
window.onload=function(){
    var newP = document.createElement('p');
    newP .innerHTML = "Hello";
    document.getElementById("Div1").appendChild(newP);
};</script>

Open in new window

This would add a div to the existing parent element without replacing existing stuff.
Or, if you are using jQuery it's even easier:
$("#Div1").append("<p>hello</p>");

Open in new window

1
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41713250
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now