-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdietQuery.js
66 lines (64 loc) · 1.76 KB
/
dietQuery.js
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
//
// dietQuery Private Functions
// ===================================================================
var _httpLoad = function (url, dietQueryInstance) {
// Credit to Joan and Félix Gagnon-Grenier on StackOverflow at question answer:
// https://stackoverflow.com/a/4033310
var xmlHttp = new XMLHttpRequest()
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
dietQueryInstance.html(xmlHttp.responseText)
}
}
xmlHttp.open('GET', url, true)
xmlHttp.send(null)
}
// dietQuery Public Functions
// ===================================================================
var $dq = {
nodeList: [],
constructor: function (selector) {
var retVal
try {
retVal = document.querySelectorAll(selector)
} catch (e) {
retVal = []
} finally {
this.nodeList = retVal
}
return this
},
style: function (property, value) {
for (var i = 0; i < this.nodeList.length; i++) {
this.nodeList[i].style[property] = value
}
return this
},
html: function (value) {
for (var i = 0; i < this.nodeList.length; i++) {
this.nodeList[i].innerHTML = value
}
return this
},
on: function (event, callback) {
for (var i = 0; i < this.nodeList.length; i++) {
this.nodeList[i].addEventListener(event, callback)
}
return this
},
attr: function (property, value) {
for (var i = 0; i < this.nodeList.length; i++) {
this.nodeList[i][property] = value
}
return this
},
load: function (url) {
_httpLoad(url, this)
return this
}
}
// dietQuery Interface
// ===================================================================
function dq (selector) { // eslint-disable-line no-unused-vars
return $dq.constructor(selector)
}