Solved

using Javascript to render a div tag

Posted on 2016-07-14
3
37 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

22 Experts available now in Live!

Get 1:1 Help Now