-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
102 lines (83 loc) · 2.86 KB
/
functions.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
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
var jsLibrary = function () {
// this.xmlhttp = new XMLHttpRequest();
};
jsLibrary.constructor = jsLibrary;
// change classname of domelement by id
jsLibrary.prototype.changeClassName = function (elemID, newclassName) {
document.getElementById(elemID).className = newclassName;
}
// get datasets from dom element
jsLibrary.prototype.getDatasetsByelemID = function(elemID, dataset) {
return document.getElementById(elemID).getAttribute(dataset);
}
// inject new dom element
jsLibrary.prototype.injectNewDomElem = function(newElem) {
const newDomElem = document.createElement(newElem);
newDomElem.innerHTML = "I am injected by Javascript";
document.body.appendChild(newDomElem);
}
// ajax and get request | or can be done by XHR
jsLibrary.prototype.ajaxAndGetRequest = function (url) {
fetch(url, {
method: 'get'
})
.then(response => response.json())
.then(jsonData => console.log(jsonData))
.catch( err => {
console.log(err)
})
}
// get set value from input, checkbox, select
jsLibrary.prototype.getSetValInputType = function( elemID, inputType, getORset, value = null ) {
switch (inputType) {
case "input":
case "dropdown":
if( getORset == "set" ){
document.getElementById(elemID).value = value;
}else{
return document.getElementById(elemID).value;
}
break;
case "checkbox":
if( getORset == "set" ){
document.getElementById(elemID).checked = true;
}else{
return document.getElementById(elemID).checked;
}
break;
default:
return;
break;
}
}
// Question 2
// 3 post request which resolved all at the same time
jsLibrary.prototype.threePostRequestResolveSameTime = function() {
const fetch1 = fetch('https://dummyjson.com/products/add', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({title: 'BMW Pencil 1'})
}).then(response => response.json())
const fetch2 = fetch('https://dummyjson.com/products/add', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({title: 'BMW Pencil 2'})
}).then(response => response.json());
const fetch3 = fetch('https://dummyjson.com/products/add', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({title: 'BMW Pencil 3'})
}).then(response => response.json())
const allData = Promise.all([fetch1, fetch2, fetch3]);
allData.then((res) => console.log(res));
}
var JSL = new jsLibrary();