- How to append data to
- Approach
- Example
- Element: append() method
- Syntax
- Parameters
- Return value
- Exceptions
- Examples
- Appending an element
- Appending text
- Appending an element and text
- The append method is unscopable
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Adding content to a div from javascript
- 4 Answers 4
- Javascript append data in div
- # Table of Contents
- # Append text to an Element (DIV, SPAN, P) in JavaScript
- # Append text to an element (DIV, SPAN, P) using insertAdjacentText()
- # Append HTML to an element (DIV, SPAN, P) in JavaScript
How to append data to
We can append data to an element using JavaScript by using the .innerHTML property to add content to the element. This can be done by using the += operator to add the new content to the existing content within the . We can also use the .appendChild() method to add a new child element to the .
is a block-level element that is used to create a division or section in an HTML document. The element is usually used to group elements together, such as paragraphs, headings, or images. The element can also be used to style a section of the document, such as adding a background color or border.
Approach
To append data to a div element using JavaScript, you would first need to select the element using a selector, then use the append() or appendChild() method to append data to the selected element.
Here is an example of how you would append a paragraph element to a div element −
var div = document.querySelector('div'); var p = document.createElement('p'); p.textContent = 'This is some data that will be appended to the div element'; div.appendChild(p);
Example
Assuming you have a div with an id of “content” −
I existed before
Element: append() method
The Element.append() method inserts a set of Node objects or string objects after the last child of the Element . String objects are inserted as equivalent Text nodes.
- Element.append() allows you to also append string objects, whereas Node.appendChild() only accepts Node objects.
- Element.append() has no return value, whereas Node.appendChild() returns the appended Node object.
- Element.append() can append several nodes and strings, whereas Node.appendChild() can only append one node.
Syntax
append(param1) append(param1, param2) append(param1, param2, /* … ,*/ paramN)
Parameters
A set of Node or string objects to insert.
Return value
Exceptions
Thrown when the node cannot be inserted at the specified point in the hierarchy.
Examples
Appending an element
let div = document.createElement("div"); let p = document.createElement("p"); div.append(p); console.log(div.childNodes); // NodeList []
Appending text
let div = document.createElement("div"); div.append("Some text"); console.log(div.textContent); // "Some text"
Appending an element and text
let div = document.createElement("div"); let p = document.createElement("p"); div.append("Some text", p); console.log(div.childNodes); // NodeList [ #text "Some text",]
The append method is unscopable
The append() method is not scoped into the with statement. See Symbol.unscopables for more information.
let div = document.createElement("div"); with (div) append("foo"); > // ReferenceError: append is not defined
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Adding content to a div from javascript
I would like to load a script that contains the content of a div on my html page. I know this is possible, but I am struggling to understand how to do it. Here is my html:
What would script.js have to be for the html page to display something? I was thinking something like this (I know it’s incorrect, I’m a beginner at javascript.
function showText() < $("#helloWorld").html("Hello World!
") $("#helloWorld").show(); > showText()
4 Answers 4
$("#helloWorld").html(" Hello World!
") $("#helloWorld").show();
and you don’t need $(«#helloWorld»).show(); .
function showText() < document.getElementById("helloWorld").innerHTML="Hello World!
"; > showText()
But remember to call showText() after the loading of the div in the DOM! (For example, call it just before