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

Instead of hover, how can i make on click?

HI,

If you look on this codepen , if you hover over the links, and the submenu appears, and you take the mouse off that, it disapear.

How can i make so i need to click the menu, and it will stay like that, no matter if the mouse is off that? Like bootstrap has. Im trying to recreate bootstrap basically. On click show, on click hide.

2 Answers

What you want is the slideToggle() method. Also, add display: none; to your .sub-menu class in css if you want it hidden by default.

$(document).ready(function() {
    $('.dropdown').on('click', function() {
        $(this).children('.sub-menu').slideToggle(200);
    });
});

Thank you xdd soo simple.

What about if i click away from it, and it disapears? I mean, right now it works only on the mother link if i click it shows etc.. what about if i click on the screen for exmapel? to hide it.

Oh, now i got a problem that when i click the input, it slides up : p how can i prevent it?

input prob.

$('input').click(function (e) {
    e.stopPropagation();
    $(this).focus();
});

What about if i click away from it, and it disapears?

create an element(any element let's say div with id hide) in your html and bind your click handler to it and hide your submenu.

And for the record, input problem, not my solution :P

Thank you :) I understand. Step by step. :D