JavaScript DOM Scripting By Example Editing and Filtering Names Filter Invitees Who Have Not Responded

Anthony Ogounchi
Anthony Ogounchi
Full Stack JavaScript Techdegree Student 5,169 Points

Every invitees' name is hidden when I check the filtering box

The code seems to be working fine, no bugs, however when I check the filtering box every name get hidden. Here's the code :

const form = document.getElementById('registrar');
const input = form.querySelector('input');
const mainDiv = document.querySelector('.main');
const ul = document.getElementById('invitedList');

const div = document.createElement('div');
const filterLabel = document.createElement('label');
const filterCheckbox = document.createElement('input');

filterLabel.textContent = "Hide those who haven't responded";
filterCheckbox.type = 'checkbox';
div.appendChild(filterLabel);
div.appendChild(filterCheckbox);
mainDiv.insertBefore(div, ul);

// An event listener on the filterCheckbox allowing to filter out the 
// not confirmed invitees' names
filterCheckbox.addEventListener('change', (e) => {
    const isChecked = e.target.checked;
    const lis = ul.children;
    if (isChecked) {
        for (let i = 0; i < lis.length; i++) {
            let li = lis[i];
            if (li.className === 'responded') {
                li.style.display = '';
            } else {
                li.style.display = 'none';
            }
        }
    } else {
        for (let i = 0; i < lis.length; i++) {
            let li = lis[i];
            li.style.display = '';
        }
    }
});


function createLI(text) {
    const li = document.createElement('li');
    li.textContent = text;
    const label = document.createElement('li');
    label.textContent = 'Confirmed';
    const checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    label.appendChild(checkbox);
    li.appendChild(label);

    const editButton = document.createElement('button');
    editButton.type = 'edit';
    li.appendChild(editButton);

    const removeButton = document.createElement('button');
    removeButton.type = 'remove';
    li.appendChild(removeButton);

    return li;
}

form.addEventListener('submit', (e) => {
    e.preventDefault();
    const text = input.value;
    // Whenever the input var value is assign to text, 
    // we then want to empty the input var content. 
    input.value = '';
    const li = createLI(text);
    ul.appendChild(li);
});
// what happens when the checkbox is checked
ul.addEventListener('change', (e) => {
    const checkbox = event.currentTarget;
    const checked = checkbox.checked;
    const listItem = checkbox.parentNode.parentNode;
    if (checked) {
        listItem.className = 'responded';
    } else {
        listItem.className = '';
    }
});
// what happens when the remove button is clicked
ul.addEventListener('click', (e) => {
    if (e.target.tagName === 'BUTTON') {
        const button = e.target;
        const li = e.target.parentNode;
        const ul = li.parentNode;
        if (button.textContent === 'remove') {
            ul.removeChild(li);
        } else if (e.target.textContent === 'edit') {
            const span = li.firstElementChild;
            const input = document.createElement('input');
            input.type = 'text';
            input.value = span.textContent;
            li.insertBefore(input, span);
            li.removeChild(span);
            button.textContent = 'save';
        } else if (e.target.textContent === 'save') {
            const input = li.firstElementChild;
            const span = document.createElement('span');
            span.textContent = input.value;
            li.insertBefore(span, input);
            li.removeChild(input);
            button.textContent = 'edit';
        }
    }
});
Steven Parker
Steven Parker
170,248 Points

If you make a snapshot of your workspace and post the link to it here, we could see the issue in operation.

2 Answers

Steven Parker
Steven Parker
170,248 Points

I did look the code over but didn't spot anything obvious, and I'd drop by and help if you were next door. :wink:
But if you upload the project into a workspace and provide a snapshot link, I can check it out that way.

Steven Parker
Steven Parker
170,248 Points

So did you ever resolve your issue?