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

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

click twice?

Hi,

I've added a toggle function to my portfolio site on small screens, but it seems like there is a problem because to show the list you need to click the button 2 times.

Any idea why?

const toggleList = document.querySelector('#toggleList');
const projectList = document.querySelector('#projectList');
const weaterList = document.querySelector('.weather-list');
const quoteList = document.querySelector('.quote-list');
const wikiList = document.querySelector('.wiki-list');
const rpsList = document.querySelector('.rps-list');
const weatherApp = document.querySelector('.weather-app');
const quoteApp = document.querySelector('.quote-app');
const wikiApp = document.querySelector('.wikipedia-app');
const rpsApp = document.querySelector('.rps-app');

toggleList.addEventListener('click', () => {
  if (projectList.style.display == 'none') {
    toggleList.textContent = 'Hide Projects';
    projectList.style.display = 'block';
  } else {
    toggleList.textContent = 'Show Projects';
    projectList.style.display = 'none';
  }
});

// toggle between projects on small screens
projectList.addEventListener('click', (e) => {
  if (e.target == weaterList) {
    quoteApp.style.display = 'none';
    wikiApp.style.display = 'none';
    rpsApp.style.display = 'none';
    weatherApp.style.display = 'block';
  }
  if (e.target == quoteList) {
    weatherApp.style.display = 'none';
    wikiApp.style.display = 'none';
    rpsApp.style.display = 'none';
    quoteApp.style.display = 'block';
  }
  if (e.target == wikiList) {
    weatherApp.style.display = 'none';
    quoteApp.style.display = 'none';
    rpsApp.style.display = 'none';
    wikiApp.style.display = 'block';
  }
  if (e.target == rpsList) {
    weatherApp.style.display = 'none';
    quoteApp.style.display = 'none';
    wikiApp.style.display = 'none';
    rpsApp.style.display = 'block';
  }
});
<button id="toggleList" class="btn btn-primary center-block">Show Projects</button>
        <div id="projectList" class="row">
          <div class="col-xs-4 col-xs-offset-4 btn btn-primary weather-list lists">WeaterApp</div>
          <div class="col-xs-4 col-xs-offset-4 btn btn-primary quote-list lists">QuoteApp</div>
          <div class="col-xs-4 col-xs-offset-4 btn btn-primary wiki-list lists">WikiApp</div>
          <div class="col-xs-4 col-xs-offset-4 btn btn-primary rps-list lists">Rock...App</div>
        </div>

2 Answers

Steven Parker
Steven Parker
229,771 Points

:point_right: It works every time for me.

The only oddity I noticed is that it says "Show Projects" before and after the first click, but that's just because of the initial text in the HTML. It should probably start out as "Hide Projects".

How are you testing it? Anything unusual there?

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Hmm.. That's odd - I only experience the double-click issue (and only the first time)

Right now I'm just testing by opening the file in the browser -> open the chrome dev tool -> setting view to iPhone 5 -> then clicking the button.