-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
60 lines (50 loc) · 1.51 KB
/
Copy pathmain.js
File metadata and controls
60 lines (50 loc) · 1.51 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
// Get Date
const date = new Date();
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear().toString().substring(2);
const now = day + '/' + month + '/' + year;
// Instanciate data array
const data = [];
const main = document.querySelector('.main');
// Iteriate data into main element
// Get and display number of articles
const count = document.querySelector('.count');
count.innerHTML = main.childElementCount;
const button = document.querySelector('.add-note');
const addForm = document.querySelector('.add-form');
const form = document.querySelector('#form');
// Add Note in data
form.onsubmit = (e) => {
e.preventDefault();
let title = document.querySelector('#form-title');
let body = document.querySelector('#body');
data.push({
title: now + ': ' + title.value,
body: body.value,
});
const newData = data[data.length - 1]
console.log(newData);
const article = document.createElement('article');
const h2 = document.createElement('h2');
const pBody = document.createElement('p');
main.appendChild(article);
article.appendChild(h2);
h2.innerHTML = newData.title;
article.appendChild(pBody);
pBody.innerHTML = newData.body;
count.innerHTML = main.childElementCount;
title.value = ''
body.value = ''
addForm.style.display = 'none';
};
// display form
button.onclick = () => {
addForm.style.display = 'block';
};
// close form display
window.onclick = (e) => {
if (e.target == addForm) {
addForm.style.display = 'none';
}
};