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

Otto linden
Otto linden
5,853 Points

Why do I get this error: Uncaught SyntaxError: Unexpected token else app.js:26 ?

"use strict";
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);

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');
    const span = document.createElement('span');
    span.textContent = text;
    li.appendChild(span);
    const label = document.createElement('label');
    label.textContent = "Confirmed";
    const checkbox = document.createElement('input');
    checkbox.type = "checkbox";
    label.appendChild(checkbox);
    li.appendChild(label);

    const editButton = document.createElement('button');
    editButton.textContent = "Edit";
    li.appendChild(editButton);

    const removeButton = document.createElement('button');
    removeButton.textContent = "Remove";
    li.appendChild(removeButton);

    buttons();
    return li;
}

function buttons(text) {
   ul.addEventListener('click', (e)  => {
    if (e.target.tagName == "BUTTON") {
        const button = e.target;
        const li = button.parentNode;
        const ul = li.parentNode;
        if (button.textContent == "Remove") {
            ul.removeChild(li);
    } else if (button.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 (button.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";
    }
    }
}); 
}

form.addEventListener('submit', (e) => {
    e.preventDefault();
    const text = input.value;
    input.value = "";
    const li = createLI(text);
    ul.appendChild(li);
});

ul.addEventListener("change", (e) => {
    const checkbox = event.target;
    const checked =  checkbox.checked;
    const listItem = checkbox.parentNode.parentNode;

    if (checked) {
        listItem.className = 'responded';
        } else {
        listItem.className = '';
        }
});
Mike Hatch
Mike Hatch
14,939 Points

I'm going to Comment instead of Answer here because I'm still learning to understand the DOM more deeply. But I do see some syntax and inconsistency issues with your code. In your if/else if statements you're using semicolons in the wrong places. You're also arranging them out of order. I'd advise going through the Conditionals Workshop to reinforce syntax rules.

Mike Hatch
Mike Hatch
14,939 Points

Hi Otto, my subscription is paused right now, and I don't have access to the video. The instructor has put a semicolon after the if conditional? As in: if (li.className == "responded");?

2 Answers

Otto linden
Otto linden
5,853 Points

Oh, my bad Mike! I didn't see that one! So no, thanks!

Otto linden
Otto linden
5,853 Points

Am I? That's the same as in the videos!?