-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaginput.js
More file actions
85 lines (60 loc) · 1.85 KB
/
taginput.js
File metadata and controls
85 lines (60 loc) · 1.85 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
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
let tags = []
let tagContainer = document.querySelector('.tag-container')
let input = tagContainer.querySelector('input')
// Adiciona Evento de adicionar e sair do input
input.addEventListener('keyup', addTags)
function addTags(event){
// Verifica se o valor adicionado é Enter
const keyPressedIsEnter = event.key == 'Enter'
if(keyPressedIsEnter){
// Faz a separação por virgulas
input.value.split(',').forEach( tag => {
// Retira valores vazios
if(tag){
// Salva no array de tags
tags.push(tag.trim()) // trim - retira espaços em branco
}
})
//
updateTags()
// Limpa o campo em tela
input.value = ""
}
//console.log(input.value)
}
function updateTags(){
clearTags()
tags.slice().reverse().forEach(tag =>{
// Adiciona o valor no elemento do Html
tagContainer.prepend(creatTag(tag))
})
}
function creatTag(tag){
// Cria elemento em Html
const div = document.createElement('div')
div.classList.add('tag')
// Cria span que conterá o valor
const span = document.createElement('span')
span.innerHTML = tag // Adiciona o valor ao span
div.append(span) // Coloca o span dentro da DIV
// Cria botão para deletar uma tag
const i = document.createElement('i')
i.classList.add('close') // Adiciona a clase ao botão
i.setAttribute('data-id', tag) // Adiciona um identificador para cada TAG
i.onclick = removeTag // Adiciona função de remover a TAG
span.append(i) // Adiciona dentro do Span
return div
}
function removeTag(event){
const buttonX = event.currentTarget // Identifica que foi clicado no botão
const id = buttonX.dataset.id // Identifica a TAG que foi clicada
const index = tags.indexOf(id) // Busca o ID no array de tags
// Retira a tag do array de tags
tags.splice(index, 1,)
updateTags()
}
function clearTags(){
tagContainer
.querySelectorAll('.tag')
.forEach(tagElement => tagElement.remove())
}