-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (40 loc) · 1.18 KB
/
index.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
const save = document.getElementById("save-btn")
let notes = []
const list = document.getElementById("list")
const input = document.getElementById("input")
const clear = document.getElementById("clear-btn")
const notes_from_storage = JSON.parse(localStorage.getItem("notes"))
if (notes_from_storage) {
notes = notes_from_storage
renderNotes()
}
save.addEventListener("click", function () {
if (input.value != "") {
notes.push(input.value)
input.value = ""
localStorage.setItem("notes", JSON.stringify(notes))
renderNotes()
}
})
clear.addEventListener("click", function () {
localStorage.clear()
notes = []
renderNotes()
})
function renderNotes() {
let list_items = ""
for (let i = 0; i < notes.length; i++) {
list_items += `
<li>
${notes[i]}
</li>
`
}
list.innerHTML = list_items
}
var inputtext = document.querySelector('input'); // get the input element
input.addEventListener('input', resizeInput); // bind the "resizeInput" callback on "input" event
resizeInput.call(inputtext); // immediately call the function
function resizeInput() {
this.style.width = this.value.length + "chr";
}