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

Rachel Cowles
Rachel Cowles
7,033 Points

Error "Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': the node before is not a child of this node

I've copied Guil's code exactly, but I keep getting the following error: Error "Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': the node before which the new node is to be inserted is not a child of this node.

const form = document.getElementById("registrar");
const input = form.querySelector("input");
const ul = document.getElementById("invitedList");
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);
    return li;

}

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 = "";
    }

});

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 ipnut = li.firstElementChild;
        const span = document.createElement("span");
        span.textContent = input.value;
        li.insertBefore(span, input);
        li.removeChild(input);
        button.textContent = "edit";
    }
  }
});

2 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

I can't test it to be sure, but I suspect your only problem is with the misspelling of input in your last else if clause:

            const ipnut = li.firstElementChild;

Everyone's favorite kind of problem--when you realize you've wasted hours of your life on a stupid typo.

Rachel Cowles
Rachel Cowles
7,033 Points

Thank you both -- wasted a lot of time on this for a stupid typo, but I wouldn't of seen it without your help. I really appreciate it!