JavaScript’s Document.append()
method inserts a set of Node
or string objects after the last child of the Document
. It is similar to the Element.append()
method used with Element
objects.
Syntax
Document.append(parameter1)
Document.append(parameter1, parameter2, /* … , */ parameterN)
Parameters
One or more Node
or string objects.
Return value
The append()
method does not have a return value, which means that the method implicitly returns undefined
.
Exceptions
HierarchyRequestError
DOMException
Thrown when the node cannot be inserted at the specified point in the hierarchy.
Examples
Here is an example of using the JavaScript append()
method.
Suppose that we should create a new HTML document to which we should add the root element (<html>
) with JavaScript.
What should we do?
How about creating a new document object and the element and using the append()
method?
let d = new Document();
let html = document.createElement('html');
d.append(html);
The reason why our document variable is d
and not document
is that the latter variable has already been declared.
Now our HTML looks like this:
<html></html>
Browser compatibility
Chrome
|
Edge
|
Firefox
|
Internet Explorer
|
Opera
|
Safari
|
Chrome (Android)
|
Firefox (Android)
|
Opera (Android)
|
Safari (iOS)
|
Samsung Internet
|
WebView (Android)
|
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
append() |
54 | 17 | 49 | 41 | 10 | 54 | 49 | 41 | 10 | 6.0 | 54 |