Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using Javascript to render a div tag

Posted on 2016-07-14
3
Medium Priority
?
75 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 1000 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

927 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