Michael
Michael

Reputation: 307

Make jQuery dropdown toggle when click away?

I am using a jQuery drop down and I want the box to toggle when you click off the box

$(document).ready(function () {
  $('li.menu_head').mouseenter(function () {
    $('div.newsadviceDrop').slideToggle('medium');
  });
});

Can you help me?

Upvotes: 2

Views: 1632

Answers (1)

jrbalsano
jrbalsano

Reputation: 844

I had to do something similar and the way I worked it out was to do something like

$('div.newsadviceDrop').click(function (e) {
    e.stopPropagation();
});
$(document).click(function() {
    ('div.newsadviceDrop').slideToggle('medium');
});

Basically, you need to toggle the menu if any other place is clicked, but if the actual location is clicked, you need to prevent the event from bubbling up to perform a toggle.

Upvotes: 3

Related Questions