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 Adding and Removing Names RSVP Checkbox

Tylan Coleman
seal-mask
.a{fill-rule:evenodd;}techdegree
Tylan Coleman
Python Development Techdegree Student 5,186 Points

Unable to get property 'addEventListener' of undefined or null reference. Unable to get property 'appendChild' of undefi

When I upload the code unable to create checkbox.

const form = document.getElementById('registrar'); const input = form.querySelector('input'); const ul = document.getElementById('invitedList');

function createLI(text){ const li = document.createElement('li'); li.textContent = text; const label = document.createElement('label'); label.textContent = 'Confirmed'; const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; label.appendChild(checkbox); li.appendChild(label); const button = document.createElement('button'); button.textContent = 'remove'; li.appendChild(button); 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 li = e.target.parentNode; const ul = li.parentNode; ul.removeChild(li); } });

3 Answers

Steven Parker
Steven Parker
231,236 Points

This code is missing a closing brace and parenthesis at the end, but otherwise seems to be OK. But for a complete analysis, it needs to be tested along with the other code parts.

To share the entire environment, make a snapshot of your workspace and post the link to it here.

Steven Parker
Steven Parker
231,236 Points

Now that I've seen the HTML component, the problem is from loading the script at the wrong time.

Normally, the script tag(s) should be the very last item(s) in the body of the document.

Steven Parker
Steven Parker
231,236 Points

Tylan Coleman — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!