UNPKG

1.54 kBMarkdownView Raw
1## dom-tree
2
3Library to manipulate DOM elements
4
5Example:
6
7```js
8var dom = require('dom-tree')
9
10dom.add(document.body, '<h1>{title}</h1>{content}', {
11 title: 'Hello',
12 content: 'Welcome!'
13})
14```
15
16## Install
17
18```bash
19$ npm install dom-tree
20```
21
22## API
23
24### add(element, child)
25
26Adds `child` to `el`
27
28```js
29add(document.body, document.createElement('textarea'))
30add('body .content', document.createElement('textarea'))
31add('.content', '<div>hello</div>')
32add('.content', '<h1>{title}</h1>', { title: 'Hello!' })
33```
34
35### addAfter(parent, child, reference)
36
37Similar to `addBefore`
38
39### addBefore(parent, child, reference)
40
41```js
42addBefore(document.body, document.createElement('textarea'), document.body.firstChild)
43addBefore('body', '<h1>{msg}</h1>', { msg: 'foobar' }, document.body.firstChild)
44```
45
46### insert(element, parent)
47
48insert `element` to `parent` as child
49
50```js
51insert(document.createElement('textarea'), document.body)
52insert('<input />', '.content')
53insert('<h1>{title}</h1>', { title: 'hello' }, '.content')
54```
55
56### replace(parent, target, replacement)
57
58replace `target` with `replacement`
59
60```js
61replace(document.body, document.body.firstChild, document.createElement('textarea'))
62replace('body .content', '.content ul', '<h1>hello</h1>')
63replace('body .content', '.content ul', '<h1>{msg}</h1>', { msg: 'hello!' })
64```
65
66### remove(element)
67
68remove `element`
69
70```js
71remove(document.body.firstChild)
72remove('body .content')
73```
74
75### remove(parent, child)
76
77remove `child`
78
79```js
80remove(document.body.firstChild, 'h1')
81```