Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add content to html body using JS?

Tags:

javascript

I have many <section> in my html <body> and want to add more through javascript. However, using innerHTML is just replacing my existing sections with new ones, instead of adding them to the old ones.

What else can I use?

like image 433
apr Avatar asked Nov 21 '12 14:11

apr


People also ask

How do you add text to your body in HTML?

The HTML <body> text Attribute is used to define a color for the text in the Document. Note: The <body> text attribute is not supported by HTML5. Instead of using this attribute, we can use css color property. color_name: It specify the name of the color for the text in the Document.

Can you append to innerHTML in JavaScript?

To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.


1 Answers

You can use

document.getElementById("parentID").appendChild(/*..your content created using DOM methods..*/) 

or

document.getElementById("parentID").innerHTML+= "new content" 
like image 197
Sajjan Sarkar Avatar answered Sep 20 '22 11:09

Sajjan Sarkar