-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
123 lines (104 loc) · 3.02 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
document.getElementById('getText').addEventListener('click', getText);
document.getElementById('getJson').addEventListener(
'click', getJson
);
document.getElementById('getPosts').addEventListener(
'click', getPosts
);
document.getElementById('addPost').addEventListener(
'submit', addPost
);
function getText(){
console.log('getText')
fetch('sample.txt')
.then((res) => res.text()
.then((data) => document.getElementById('txt').innerHTML = data)
)
}
function getJson(){
console.log('getJson')
fetch('users.json')
.then((res) => res.json())
.then((data) => {
let output = '<h3>Users</h3>';
data.forEach(function(element) {
console.log(element)
output += `
<ul>
<li>ID: ${element.id}</li>
<li>Name: ${element.name}</li>
<li>Email: ${element.email}</li>
</ul>
`
});
document.getElementById('txt').innerHTML = output
})
}
function getPosts(){
console.log('getPosts')
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => {
let output = '<h3>Users</h3>';
data.forEach(function(element) {
console.log(element)
output += `
<div>
<h4>Title: ${element.title}</h4>
<p> ${element.body}</p>
</div>
`
});
document.getElementById('txt').innerHTML = output
})
}
function addPost(e){
e.preventDefault();
let title = document.getElementById('title').value;
let body = document.getElementById('body').value;
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers : {
'Accept': 'application/json, text/plain, */*',
'Content-type': 'application/json'
},
body: JSON.stringify({title: title, body: body}),
})
.then((res) => res.json())
.then((data) => console.log(data))
}
// global function fectch document by Id
function id(params) {
return document.getElementById(params);
}
// get modal element
let modal = id('simpleModal');
// get modal btn
let modalBtn = id('modalBtn');
// get close btn
let closeBtn = document.getElementsByClassName('closeBtn')[0];
let interviewButton = id('interview');
function clickOutside(e) {
if(e.target == modal){
modal.style.display = "none"
}
}
// function open modal
function openModal(){
modal.style.display = "block";
}
// function to clse
function closeModal(){
modal.style.display = "none";
}
function viewButton(){
fetch('http://www.ebmeds.org/compiler/api/missiondata')
.then((res) => console.log(res.json()))
.then((data) => console.log(data))
}
// listen top click
modalBtn.addEventListener('click', openModal);
// listen for close click
closeBtn.addEventListener('click', closeModal);
// listen for outside click to close modal
window.addEventListener('click', clickOutside)