jeudi 18 février 2021

if-else does not work to clear a list or to alert if list is already clear

[BEGINNER] I want to clear my list after clicking a button (which I managed to do) and I want to create an IF statement. IF the UL has LI's then clear list. If UL does not have LI's then I want to alert('List is already clear'). The code below doesnt work, I'm not sure what I'm doing wrong. Thank you for you help.

const clearBtn = document.querySelector('.clear-btn');
const list = document.querySelector('.list');
clearBtn.addEventListener('click', clearList);


function clearList() {
    if (list.hasChildNodes = true) {
        list.innerHTML = ''
    } else {
        alert('List is already clear');
    }
}

Aucun commentaire:

Enregistrer un commentaire