-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
107 lines (78 loc) · 2.09 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
title: Element.prepend()
slug: Web/API/Element/prepend
page-type: web-api-instance-method
tags:
- API
- DOM
- Method
- Node
- Element
- Reference
- prepend
browser-compat: api.Element.prepend
---
{{APIRef("DOM")}}
The **`Element.prepend()`** method inserts a set of
{{domxref("Node")}} objects or string objects before the first child
of the {{domxref("Element")}}. String objects are inserted as
equivalent {{domxref("Text")}} nodes.
## Syntax
```js
prepend(param1)
prepend(param1, param2)
prepend(param1, param2, /* … ,*/ paramN)
```
### Parameters
- `param1`, …, `paramN`
- : A set of {{domxref("Node")}} or string objects to insert.
### Return value
None ({{jsxref("undefined")}}).
### Exceptions
- `HierarchyRequestError` {{DOMxRef("DOMException")}}
- : Thrown when the node cannot be inserted at the specified point in the hierarchy.
## Examples
### Prepending an element
```js
let div = document.createElement("div");
let p = document.createElement("p");
let span = document.createElement("span");
div.append(p);
div.prepend(span);
console.log(div.childNodes); // NodeList [ <span>, <p> ]
```
### Prepending text
```js
let div = document.createElement("div");
div.append("Some text");
div.prepend("Headline: ");
console.log(div.textContent); // "Headline: Some text"
```
### Prepending an element and text
```js
let div = document.createElement("div");
let p = document.createElement("p");
div.prepend("Some text", p);
console.log(div.childNodes); // NodeList [ #text "Some text", <p> ]
```
### The prepend method is unscopable
The `prepend()` method is not scoped into the `with` statement.
See {{jsxref("Symbol.unscopables")}} for more information.
```js
let div = document.createElement("div");
with(div) {
prepend("foo");
}
// ReferenceError: prepend is not defined
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("Element.append()")}}
- {{domxref("Node.appendChild()")}}
- {{domxref("Node.insertBefore()")}}
- {{domxref("Element.before()")}}
- {{domxref("Element.insertAdjacentElement()")}}
- {{domxref("NodeList")}}