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

Chirag Mehta
Chirag Mehta
15,332 Points

Made 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.

Can you post the snapshot?

1 Answer

Steven Parker
Steven Parker
229,732 Points

This 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
Chirag Mehta
15,332 Points

Hey 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