The input where i get the value is attached with the keyPressed function, so when the person clicks the event runs, when i try to make a second task, the event does not fires.
const container = document.getElementById('container');
document.getElementById("textNode").addEventListener("keypress", keyPressed);
let button = document.querySelectorAll('button');
function keyPressed() {
if (event.which === 13) {
if (this.value == '') {
return alert('Não é possivel inserir vazio')
}
console.log('stuff')
let text = this.value;
let novaTarefa = `<div class="task">
<h2>${text}</h2>
<button onclick="deleteThis">feita!</button>
</div>`;
container.innerHTML += novaTarefa;
}
}
i can create a new task once, but then it appears as if the reference were lost,
However, but it does works with this other code of mine:
function keyPressed() {
if (event.which === 13) {
if(this.value == ''){
return alert('Não é possivel inserir vazio')
}
let h2 = document.createElement("h2");
let text = document.createTextNode(this.value);
let btn = document.createElement('button');
let textBtn = document.createTextNode("feita!");
h2.appendChild(text);
btn.appendChild(textBtn);
let att = document.createAttribute("class");
att.value = "task";
let div = document.createElement("div");
div.appendChild(h2);
div.appendChild(btn);
div.setAttribute('class', 'task')
container.appendChild(div);
text = '';
}
}