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 trialSiddharth Pandey
7,280 PointsCan someone explain what is wrong with my code
Okay, their are definitely some things wrong with my code, I was trying to re-factor it, and it's really not working, can someone tell me the correct answer and explain everything I did wrong, there is a lot, I know. However, I would really appreciate it if someone took the time to help me.
const form = document.querySelector('#registrar');
const input = form.querySelector('input');
const ul = document.querySelector('#invitedList');
const main = document.querySelector('.main');
const div = document.createElement('div');
const label = document.createElement('label');
const filter = document.createElement('input');
main.insertBefore(div, ul);
div.appendChild(label);
div.appendChild(filter);
filter.type = 'checkbox';
label.textContent = 'Show Selected Items';
filter.addEventListener('change', function filter(e) {
const unchecked = ul.children;
if(e.target.tagName === 'INPUT') {
for(let i = 0; i < ul.children.length; i += 1) {
let li = ul[i];
console.log(ul.children);
if (li.className === 'responded') {
li.style.display === '';
} else {
li.style.display = 'none';
}
}
}
});
function createLI(text) {
function createElement(elementName, property, value) {
const element = document.createElement(elementName);
element[property] = value;
return element;
}
function appendElement(elementName, property, value) {
const element = createElement(elementName, property, value)
element = li.appendChild(element);
return element;
}
const text= input.value;
const li = document.createElement('li');
const span = createElement('span', '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 editButton = createElement('button', 'textContent', 'Edit')
li.appendChild(editButton);
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
li.appendChild(removeButton);
return li;
}
form.addEventListener('submit', function submit(name) {
name.preventDefault();
const text = input.value;
input.value = '';
const li = createLI(text);
ul.appendChild(li);
});
ul.addEventListener('change', function styling(e) {
const checkbox = e.target;
const checked = e.target.checked;
const listItem = checkbox.parentNode.parentNode;
if (checked) {
listItem.className = 'responded';
} else {
listItem.className = '';
}
});
ul.addEventListener('click', function remove(e) {
const listItem = e.target.parentNode;
const ul = listItem.parentNode;
const button = e.target;
const li = button.parentNode;
if (button.tagName == 'BUTTON') {
if (button.textContent === 'Remove') {
ul.removeChild(listItem);
} 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';
}
}
});
Steven Parker
231,271 PointsSteven Parker
231,271 PointsA snapshot of your workspace would make it possible to replicate your environment (and would include the complete code, including the HTML part which is not shown here).
It would also be helpful if you describe "really not working" in a bit more detail.