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

JS loop won't properly execute

For some reason my if / else loop never seems to enter the 'else' section of the loop. Any ideas?

function posterTog() {
          var poster = document.getElementsByClassName("movposter");
          var buttonLabel = document.getElementById("togButton").value
         if (buttonLabel = "Times Only") {
              for (i=0; i < poster.length; i++) {
                poster[i].style.display = "none";    
              } 
              document.getElementById("togButton").value = "Full Schedule"
          }

          else {
              for (i=0; i < poster.length; i++) {
                poster[i].style.display = "block";    
              } 
              document.getElementById("togButton").value = "Times Only"  
        }
      }

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! WIthout seeing the rest of the code nor the HTML my first guess would be this line as your problem:

if (buttonLabel = "Times Only") 

This is an assignment statement. I feel fairly sure you meant to do an equality check here:

if (buttonLabel == "Times Only")
// Note the double equals for a comparison

Let me know if this helped! :sparkles:

You're my hero! I knew it was something ridiculous I was overlooking. Thank you so much!