-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
51 lines (46 loc) · 1.37 KB
/
script.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
let tagsHandler = (() => {
const tagsInput = document.getElementById('tagsField');
const tagsBtn = document.getElementById('getTags');
const ul = document.getElementById('ul');
const tagsRemover = document.getElementsByClassName('removeTag');
let tagsContainer = [];
let getTags = () => {
if(tagsInput.value) {
tagsInput.classList.remove('hasError');
let tags = tagsInput.value.split(",");
let pure_tags = tags.map((x) => x.trim());
let validTags = pure_tags.filter((elemnt) => {
if(elemnt !== '') {
return elemnt;
}
});
tagsContainer = [...validTags];
} else {
tagsInput.classList.add('hasError');
}
}
let showTags = () => {
if(tagsContainer.length > 0) {
tagsContainer.forEach((element) => {
let container = document.createElement('LI');
container.innerHTML = '<span class="removeTag">✖ </span>'+element;
container.className = 'tags';
ul.appendChild(container);
});
tagsContainer = [];
tagsInput.value = '';
}
}
tagsInput.addEventListener('input', getTags);
tagsBtn.addEventListener('click', showTags);
setInterval(function() {
for(let i = 0; i < tagsRemover.length; i = i + 1) {
if(tagsRemover[i]) {
tagsRemover[i].onclick = () => {
const tagsElemnts = document.getElementsByClassName('tags');
tagsElemnts[i].parentNode.removeChild(tagsElemnts[i]);
}
}
}
}, 1000);
})();