Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Anthony Ogounchi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Anthony Ogounchi
Full Stack JavaScript Techdegree Graduate 14,594 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
231,269 Points

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

3 Answers

Steven Parker
Steven Parker
231,269 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
231,269 Points

So did you ever resolve your issue?