-1
Friends! the code I have is this below, but when I remove all items from the list, that is all <li>
with the function, I wanted the message of alert
, when the person presses the Clear List button again, but instead an error appears which is the following:
Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'. at HTMLInputElement.removeElement (script.js:26)
const $list = document.querySelector('ul')
const $product = document.querySelector('#product')
const $btn = document.querySelector('#btn')
const $reset = document.querySelector('#reset')
$btn.addEventListener('click', adcProduct)
function adcProduct() {
const $item = `<li>${$product.value}</li>`
$list.innerHTML += $item
$product.value = ''
$product.focus()
}
$product.addEventListener('keydown', (e) => {
if (e.keyCode === 13) {
adcProduct()
}
})
$reset.addEventListener('click', removeElement)
function removeElement() {
// Removes an element from the document
const $ul = document.getElementById("list");
$ul.removeChild($ul.children[0])
if($product = '') {
alert('A lista está vazia')
}
}
$ul.removeChild($ul.[0]) so test
– Germano Buss Niehues
Germano, your solution did not work here. Also did not understand why I was negative, because if I asked is why I did not find the solution anywhere and I have tried everything.
– Pablo Stefanes
http://devfuria.com.br/javascript/dom-remove-child/ at a glance at this link,I believe it will help you
– Germano Buss Niehues
You will have to pass which node from the list you want to remove, not the value
– Germano Buss Niehues
But there it is! I can remove the nodes/Node, what I can’t get is that after removing the nodes and not having any more to remove, the user presses the remove button again, an Alert message appears, warning that there are no more items to be removed.
– Pablo Stefanes