Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Chirag Mehta
15,331 PointsMade select element but can't figure out how to make selections hidden if responded or not
I've made the select element to give 3 choices for responses. I tidied it up with the appendToSelect func and it adds to li with no problems and replaced the checkbox.
function createLI(text) {
function createElement(elementName, property, value) {
const element = document.createElement(elementName);
element[property] = value;
return element;
}
function appendToLI(elementName, property, value) {
const element = createElement(elementName, property, value);
li.appendChild(element);
return element;
}
function appendToSelect(optionName) {
const element = createElement('option', 'textContent', optionName);
select.appendChild(element);
return element;
}
const li = document.createElement('li');
const select = document.createElement('select');
appendToLI('span', 'textContent', text);
appendToLI('label', 'textContent', 'Invitee response:')
.appendChild(select);
appendToSelect('Choose response');
appendToSelect('Attending');
appendToSelect('Not Coming');
appendToSelect('Not Responded');
appendToLI('textarea', 'rows', 5);
appendToLI('button', 'textContent', 'Edit');
appendToLI('button', 'textContent', 'Remove');
return li;
}
I then wrote this to change the class when user chooses 'Attending' or 'Not Coming' with selectedIndex. After checking dev tools I see a class is added to the li but there's no name given.
ul.addEventListener('change', (e) => {
const select = e.target;
const options = select.children;
const listItemLabel = select.parentNode;
const listItem = select.parentNode.parentNode;
for (let i = 0; i < options.length; i++) {
const option = options[i];
if (option.selectedIndex === 1 || option.selectedIndex === 2) {
listItemLabel.firstChild.textContent = 'Responded';
listItem.className = 'responded';
} else {
listItemLabel.firstChild.textContent = 'Not Responded';
listItem.className = '';
}
}
});
Here's a snapshot if anyone wants to look at the all my code. Any suggestions from the community would be great.

Chirag Mehta
15,331 PointsHere's the snapshot
1 Answer

Steven Parker
215,955 PointsThis caught my eye:
if (option.selectedIndex === 1 || option.selectedIndex === 2) {
An option element doesn't have a "selectedIndex" property, that would be found on the select element. So this test always fails, and every selection gives you "Not Responded".

Chirag Mehta
15,331 PointsHey Steven,
I've got it. Seems like I didn't need the for loop I just used the if statement and used the select element.
ul.addEventListener('change', (e) => {
const select = e.target;
const listItemLabel = select.parentNode;
const listItem = select.parentNode.parentNode;
if (select.selectedIndex === 1 || select.selectedIndex === 2) {
listItemLabel.firstChild.textContent = 'Responded';
listItem.className = 'responded';
} else {
listItemLabel.firstChild.textContent = 'Not Responded';
listItem.className = '';
}
});
Thanks
KRIS NIKOLAISEN
54,663 PointsKRIS NIKOLAISEN
54,663 PointsCan you post the snapshot?