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 One Solution

Andrew Whatmore
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Whatmore
Full Stack JavaScript Techdegree Student 4,615 Points

Solution - mouse as well as tab navigation

Adding mouse as well as tab navigation.

I've tried adding a solution which focuses on mouse clicks as well as tab navigation.

Is this the most efficient way to do it? I couldn't think of a way to combine the code at all, since I used bubbling for the mouse navigation to save memory, whereas the tab navigation uses an event listener for each checkbox.

Many thanks in advance

// Select the checkboxes
const checkboxes = document.querySelectorAll('input[type="checkbox"]');

// Select the fieldset which contains the checkbox inputs
const checkboxSet = document.querySelector('fieldset.heroes');

// FOR MOUSE NAVIGATION
// Create a variable for storing the current checkbox
let currentCheckbox;
// listen for a focus event within the fieldset
checkboxSet.addEventListener('click', (e) => {
  // if the clicked item was a checkbox
  if (e.target.type === "checkbox") {
    // clear previous checkbox
    if (currentCheckbox) {
      // remove the focus class
      currentCheckbox.classList.remove('focus');
      // Also remove the 'focus' class from the parent label element
      currentCheckbox.parentElement.classList.remove('focus');
    }
    // set currentCheckbox to the clicked item        
    currentCheckbox = e.target;
    //add the 'focus' class to the checkbox
    currentCheckbox.classList.add('focus');
    // add the 'focus' class to the parent label element
    currentCheckbox.parentElement.classList.add('focus');
  }
});

// FOR TAB NAVIGATION
// Loop through each checkbox
checkboxes.forEach(checkbox => {
// Step 3: Add an event listener for the 'focus' event to each checkbox
  checkbox.addEventListener('focus', () => {
  // Step 3.1: When a checkbox gains focus, add the 'focus' class to the checkbox
    checkbox.classList.add('focus');
  // Step 3.2: Also add the 'focus' class to the parent label element
    checkbox.parentElement.classList.add('focus');
  });

// Step 4: Add an event listener for the 'blur' event to each checkbox
  checkbox.addEventListener('blur', () => {
  // Step 4.1: When a checkbox loses focus, remove the 'focus' class
    checkbox.classList.remove('focus');
  // Step 4.2: Also remove the 'focus' class from the parent label element
    checkbox.parentElement.classList.remove('focus');
  });
});