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

jQuery - Keep selection not showing

I have a small script that when you select a specific dropdown that another field shows.

  $("#tablet_status").change(function(){
    if($(this).val() !== "4"){
      $('#passcode').hide();
    }else{
      $('#passcode').show();
    }
  });

But the thing is, when I view the page that doesn't have that value in the dropdown, the other field still shows until you select something again.

I need it to always hide unless the selected value is 4.

1 Answer

Steven Parker
Steven Parker
243,656 Points

:point_right: Make it initially hidden using CSS:

#passcode { display: none; }

You could also manually trigger the change event on load, but you'd still risk seeing it momentarily.

Lol that works..

Not sure why I didn't think of that but thanks!