Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using Javascript to render a div tag

Posted on 2016-07-14
3
Medium Priority
?
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 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