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

How to make checkbox disappear when a different checkbox is changed

I need the checkbox of the li's to disappear when the filter checkbox is changed.

document.addEventListener('DOMContentLoaded', (e) => {
const form = document.getElementById('registrar');
const input = form.querySelector('input');

const mainDiv = document.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 confirmed yet";
filterCheckBox.type = 'checkbox';
div.appendChild(filterLabel);
div.appendChild(filterCheckBox);
mainDiv.insertBefore(div, ul);
filterCheckBox.addEventListener('change', (e) => {
  const lis = ul.children;
  const isChecked = e.target.checked;
  if (isChecked) {
    confCheckbox.style.display = 'none';
    confLabel.style.display = 'none';
    for (let i = 0; i < lis.length; i += 1) {
      let li = lis[i];
      if (li.className === 'responded') {
        li.style.display = '';
      } else {
         li.style.display = "none";
        confCheckbox.style.display = '';
    confLabel.style.display = '';
      }
    } 
  } else {  
  for (let i = 0; i < lis.length; i += 1) {    
    const li = lis[i];
    li.style.display = '';
  }
  }
 });

function createLI(text) {
  const li = document.createElement('li');
  const span = document.createElement('span');  
  span.textContent = text;
  li.appendChild(span);

  const confCheckbox = document.createElement('input');
  const confLabel = document.createElement('label');

  confCheckbox.type = 'checkbox';
  confLabel.textContent = 'Confirmed';

  confLabel.appendChild(confCheckbox);
  li.appendChild(confLabel);  

  const editButton = document.createElement('button');
  editButton.textContent = 'edit';
  li.appendChild(editButton);

  const removeButton = document.createElement('button');
  removeButton.textContent = 'remove';
  li.appendChild(removeButton);
  return li;
}

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

       if (input.value === '') {
   alert("Must type a real name!"); 
   li.style.display = 'none';
  }

  input.value = '';
  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 button = e.target;
    const li = button.parentNode;
    const ul = li.parentNode;
    if (button.textContent === 'remove') {
      ul.removeChild(li);
    } 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';
    }
  }
});  
  });

2 Answers

Steven Parker
Steven Parker
242,796 Points

OK, I get it now. Though you might want to leave the controls there in case you want to change the confirmed status (un-check it).

Did you give this a try yourself yet? The code above is still the before-fixes code.

Anyway, the task is very similar to what is being done already, you'll just get the label element and hide it when you're not hiding the whole list item. Here's one way to do it:

    if (isChecked) {
      for (let i = 0; i < lis.length; i += 1) {
        const li = lis[i];
        const lab = li.querySelector("label");
        if (li.className === "responded") {
          lab.style.display = "none";
        } else {
          li.style.display = "none";
        }
      }
    } else {
      for (let i = 0; i < lis.length; i += 1) {
        const li = lis[i];
        const lab = li.querySelector("label");
        li.style.display = "";
        lab.style.display = "";
      }
    }
Steven Parker
Steven Parker
242,796 Points

In the event handler for the filter checkbox, there are 4 code lines that reference "confCheckbox" or "confLabel" that should be removed. These references do not exist in that scope, and cause syntax errors and stop the handler. They are not needed for the functionality anyway. (Lines 21, 22, 29 and 30.)

I'm not sure you are getting my question. I want the checkbox under the name of the invitees to disappear when I click the filter checkbox. Then I want it to reappear when I uncheck that box

Steven Parker
Steven Parker
242,796 Points

The label on the filter check box says "Hide those who haven't confirmed yet". And after the fixes, clicking on it makes all the records for people who are not confirmed disappear., and un-clicking it makes them reappear That behavior seems to fit the label description.

Are you saying you want only the confirmed checkbox to disappear, but leave the person's record? That would be very different from what the label implies.

If that's really what you want, did you want to make every other checkbox disappear? Or only the ones not already checked? And just the box, or the label also?

So, I want the program to do what is already doing, but whenever you check the filter box after it makes the unconfirmed people disappear, I want the remaining checkboxes(on the confirmed people) to disappear as well. Because you already know they are confirmed since you checked the 'hide unconfirmed' box