-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
88 lines (69 loc) · 2.3 KB
/
app.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
const input = document.querySelector('.get-note');
const addNoteBtn = document.querySelector('#add-btn');
addNoteBtn.addEventListener("click", addNewNote);
const allNotes = [];
const color = document.querySelector('.get-color');
const notesList = document.querySelector('.notes-list');
// document.addEventListener('keypress', (event) => {
// if (event.keyCode === 13) {
// addNewNote();
// }
// })
function addNewNote() {
if (input.value) {
let newNote = {
note: input.value,
noteColor: color.value
};
allNotes.push(newNote);
}
else {
alert("A note can't be empty.");
}
// console.log(newNote);
input.value = "";
input.focus();
displayNotes(allNotes);
}
function displayNotes(notes) {
notesList.innerHTML = " ";
notes.forEach(element => {
let noteHTML= `
<div class="note" id="note" style="background-color:${element.noteColor};">
<div class="note-view" id="note-view">
${element.note}
</div>
<div>
<a class="deleteBtn"><img src="icons/bin.png" class="bin-icon"></a>
<a class="copyBtn"><img src="icons/copyBtn.svg" class="copy-icon"></a>
</div>
</div>
`;
notesList.insertAdjacentHTML('afterbegin', noteHTML);
const deleteBtn = document.querySelector('.deleteBtn');
deleteBtn.addEventListener('click', e => { deleteNote(e,element);})
const copyBtn = document.querySelector('.copyBtn');
copyBtn.addEventListener('click', e => {copyNote(e,element);})
});
}
function deleteNote(e,element) {
let item = e.target.parentElement.parentElement.parentElement;
// console.log(item);
item.parentNode.removeChild(item);
// console.log(element)
allNotes.pop(element);
}
function copyNote(e,element) {
// Get the text field
console.log(e);
console.log(element);
var copyText = `${element.note}`;
navigator.clipboard.writeText(copyText);
// console.log(copyText);
// // Select the text field
// copyText.select();
// copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
// Alert the copied text
alert("Copied text: " + copyText);
}