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

Dennis Terrey
Dennis Terrey
2,527 Points

Uncaught TypeError: Cannot read property 'insertBefore' of null at app.js:13

This is driving me insane, can't for the life figure out what the issue is. Any help would be greatly appreciated!

const form = document.getElementById('registrar');
const input = form.querySelector('input');
const mainDiv = form.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);

function createLI(text) {
    const li = document.createElement('li');
    const label = document.createElement('label');
    const checkbox = document.createElement('input');
    const span = document.createElement('span');
    const editButton = document.createElement('button');
    const removeButton = document.createElement('button');
    span.textContent = text;
    li.appendChild(span);
    label.textContent = 'Confirmed';
    checkbox.type = 'checkbox';
    label.appendChild(checkbox);
    li.appendChild(label);
    editButton.textContent = 'edit';
    li.appendChild(editButton);
    removeButton.textContent = 'remove';
    li.appendChild(removeButton);
    return li;
}

form.addEventListener('submit', (e) => {
    const text = input.value;
    const li = createLI(text);
    e.preventDefault();
    input.value = '';
    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 input = li.firstElementChild;
            const span = document.createElement('span');
            span.textContent = input.value;
            li.insertBefore(span, input);
            li.removeChild(input);
            button.textContent = 'save';
        }
    }
});
Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

It looks like mainDiv contains null so the assignment of form.querySelector('.main'); hasn't worked. Is main the correct class name?

Steve.

Dennis Terrey
Dennis Terrey
2,527 Points

Steve Hunter thanks for the help! I should have really posted the HTML.

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>RSVP App</title>
  <link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
</head>
<body>
  <div class="wrapper">
    <header>
      <h1>RSVP</h1>
      <p>A Treehouse App test</p>
      <form id="registrar">
        <input type="text" name="name" placeholder="Invite Someone">
        <button type="submit" name="submit" value="submit">Submit</button>
      </form>
    </header>

    <div class="main">  
      <h2>Invitees</h2>
      <ul id="invitedList"></ul>    
    </div>
  </div>
  <script type="text/javascript" src="js/app.js"></script>
</body>
</html>

3 Answers

Steven Parker
Steven Parker
174,057 Points

Your selection starts at the wrong point in the DOM tree.

I do know a bit about JavaScript, and Steve is correct that since there is no element with class main inside the form with id "registrar", then mainDiv will be null.

But since the class main is unique on the page, you don't need a child selector and can get the correct value this way:

const mainDiv = document.querySelector('.main');

Also, on the last line of code, I think you may have meant to write "edit" instead of "save":

            button.textContent = 'edit';
Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,546 Points

Hi Dennis,

Should it be form.querySelector(".main") or rather document.querySelector(".main")? The main class isn't inside the form, so use the document? (I'm not a JS expert, I'm afraid!)

And perhaps try document.querySelector("div > .main")

Just some thoughts.

Steve.

Dennis Terrey
Dennis Terrey
2,527 Points

Thank you Steve Hunter & Steven Parker that did the trick! Really appreciate the help!