-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (63 loc) · 1.39 KB
/
index.js
File metadata and controls
70 lines (63 loc) · 1.39 KB
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
/*
* create new instance of Child
*/
function Child(el) {
this.el = el;
return this;
};
/*
* return the child at a given index
* or all, if no index is defined
*/
Child.prototype.get = function (index) {
return index !== undefined ? this.el.childNodes[index] : this.el.childNodes;
};
/*
* return all child nodes
*/
Child.prototype.getAll = function () {
return this.get();
};
/*
* add an element to a given index
* if no index is defined: append it
*/
Child.prototype.add = function (element, index) {
var referenceElement;
if (index === null || index === undefined || index > this.getAll().length) {
this.el.appendChild(element);
} else {
referenceElement = this.get(index);
this.el.insertBefore(element, referenceElement);
}
return this;
};
/*
* remove a child at given index or
* remove given child element or
* remove all child nodes
*/
Child.prototype.remove = function (arg) {
var children,
element = arg;
if (arg === undefined) {
children = this.get();
for (var i = 0; i < children.length; i++) {
this.el.childNodes.removeChild(children[i]);
}
} else {
if (arg == arg*1)
element = this.get(arg);
this.el.childNodes.removeChild(element);
}
return this;
};
/*
* remove all child nodes
*/
Child.prototype.removeAll = function () {
return this.remove();
};
module.exports = function (el) {
return new Child(el);
};