-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (63 loc) · 1.98 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
const engWord = document.getElementById("eng"),
rusWord = document.getElementById("rus"),
inputs = document.getElementsByClassName("input"),
addButton = document.getElementById("add-word-btn"),
table = document.getElementById("table");
let words,
btnsDelete;
localStorage.length < 1 ? words = [] : words = JSON.parse(localStorage.getItem("words"));
let addWordToTable = index => {
table.innerHTML += `
<tr class="tr">
<td class="eng-word">${words[index].eng}</td>
<td class="rus-word">${words[index].rus}</td>
<td>
<button class="btn-delete">X</button>
</td>
</tr>
`
};
words.forEach((item, i) => {
addWordToTable(i);
});
function CreateWord(eng, rus) {
this.eng = eng;
this.rus = rus;
}
addButton.addEventListener("click", () => {
if (
engWord.value.length < 1 ||
rusWord.value.length < 1 ||
isFinite(engWord.value) ||
isFinite(rusWord.value)
) {
for (let key of inputs) {
key.classList.add("error");
}
} else {
for (let key of inputs) {
key.classList.remove("error");
}
words.push(new CreateWord(engWord.value, rusWord.value));
localStorage.setItem("words", JSON.stringify(words));
addWordToTable(words.length - 1);
engWord.value = "";
rusWord.value = "";
addEventDelete();
}
});
let addEventDelete = () => {
if (words.length > 0) {
btnsDelete = document.querySelectorAll(".btn-delete");
for (let btn of btnsDelete) {
btn.addEventListener("click", e => {
const rowIndex = e.target.parentNode.parentNode.rowIndex;
e.target.parentNode.parentNode.parentNode.remove();
words.splice(rowIndex, 1);
localStorage.removeItem("words");
localStorage.setItem("words", JSON.stringify(words));
})
}
}
};
addEventDelete();