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';
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;
    label.textContent = 'Confirmed';
    checkbox.type = 'checkbox';
    editButton.textContent = 'edit';
    removeButton.textContent = 'remove';
    return li;

form.addEventListener('submit', (e) => {
    const text = input.value;
    const li = createLI(text);
    input.value = '';

ul.addEventListener('change', (e) => {
    const checkbox =;
    const checked = checkbox.checked;
    const listItem = checkbox.parentNode.parentNode;
    if (checked) {
        listItem.className = 'responded';
    } else {
        listItem.className = ''

ul.addEventListener('click', (e) => {
    if ( === 'BUTTON') {
        const button =;
        const li = button.parentNode;
        const ul = li.parentNode;
        if(button.textContent === 'remove') {
        } 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);
            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);
            button.textContent = 'save';
Steve Hunter
Steve Hunter
Treehouse Moderator 57,652 Points

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


Dennis Terrey
Dennis Terrey
2,527 Points

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

<!DOCTYPE html>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>RSVP App</title>
  <link href="" rel="stylesheet">
  <link href=",700" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
  <div class="wrapper">
      <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>

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

3 Answers

Steven Parker
Steven Parker
195,812 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
Steve Hunter
Treehouse Moderator 57,652 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.


Dennis Terrey
Dennis Terrey
2,527 Points

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