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

I have a toggle menu and I can't get it right with j query any help?

Hi Guys , I have jquery code for a toggle menu I want to use in my site. The following link will show you that work's. But when I place it in my site is not. Feel tired confused. thanks guys

So, it Is working here [example link]http://jsfiddle.net/CqREn/8/)

Not in my site:
[example link]http://www.carlosdiazdesigns.com/toggle-manu.html)

can someone help me out. Thanks a lot.

3 Answers

Hey Carlos,

Check your code. Try traversing the dom instead, like so.

$('#MenuToggle').on('click', function(e){
    $(this).parent().parent().find('ul').slideToggle();
    e.preventDefault();
});

This way your only Querying the dom for 1 element. Then basing what to slide toggle based on that element. If you're having a hard time trying to figure out what your code isn't working properly. Open up your console and check for errors when clicking on a button you expect something to happen. This will usually give you a better idea of what's going on.

Enjoy. :)

Hi Jake, Thanks a lot for you time. I just trued your suggestion and no luck so far.

No idea what is going on here :( I have this library in the head:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

also in the head I place the code you send me:

 <script>
   $('#MenuToggle').on('click', function(e){
    $(this).parent().parent().find('ul').slideToggle();
   e.preventDefault();
     });
  </script>

I try also placing that piece of code inside the body at the top and also before the body tag closes. And again No luck. I guess because I'm a beginner I'm having this hard time, but at the same time I can see that the code is actually no ta difficult to understand so can get why is not working. :(

here is the link again:

Put your javascript script tags just below the closing body tag like so..

 <script src="----- "></script>
 <script>
     $('#MenuToggle').on('click', function(e){
         $(this).parent().parent().find('ul').slideToggle();
         e.preventDefault();
     });
 </script>
 </body>

Is working now!!!! Great!!! Thanks Jake! Now, can you please explain a little bit why is working when I put the javascript right below the closing body tab? Thansk a lot.

you're javascript was being loaded before the dom elements we're even created. So jquery was trying to attach that 'click' event to a button that didn't even exist yet.

Start loading all of your javascripts before the closing body tag, this also speeds up load time, so the user isn't waiting for javascript to be loaded before the page is even shown. <--- this is just good UX ;)

Nice Jake. Thanks so much for such a crystal clear explanation. I really appreciate your help. Thank you, Thank you ...and Thank you. Have a great time.