How to blur the div element?

I have a dropdown menu inside a DIV.

I want the dropdown to be hide when user click anywhere else.

$('div').blur(function() { $(this).hide(); }

is not working.

I know .blur works only with <a> but in this case what is the simplest solution?



I think the issue is that divs don't fire the onfocusout event. You'll need to capture click events on the body and then work out if the target was then menu div. If it wasn't, then the user has clicked elsewhere and the div needs to be hidden.

    $("body").click(function(e) {
      if( !== 'menu'){
  <style>#menu { display: none; }</style>

  <div id="menu_button" onclick="$('#menu').show();">Menu....</div>
  <div id="menu"> <!-- Menu options here --> </div>

  <p>Other stuff</p>

Try using tabindex attribute on your div, see:

Check this post for more information and demo.

$("body").click(function (evt) {
     var target =;
     if( !== 'menuContainer'){

give the div an id, for instance "menuContainer". then you can check by instead of target.tagName to make sure its that specific div.


Not the cleanest way, but instead of capturing every click event on the page you could add an empty link to your div and use it as a "focus proxy" for the div.

So your markup will change to:

<div><a id="focus_proxy" href="#"></a></div>

and your Javascript code should hook to the blur event on the link:

$('div > #focus_proxy').blur(function() { $('div').hide() })

Don't forget to set the focus on the link when you show the div:

$('div > #focus_proxy').focus()

I just encountered this problem. I guess none of the above can fix the problem properly, so I post my answer here. It's a combination of some of the above answers: at least it fixed 2 problems that one might met by just check if the clicked point is the same "id"

$("body").click(function(e) {
    var x =;

    //check if the clicked point is the trigger
    if($(x).attr("class") == "floatLink"){
    //check if the clicked point is the children of the div you want to show 
    else if($(x).closest(".menu").length <= 0){

.click will work just fine inside the div tag. Just make sure you're not over top of the select element.

$('div').click(function(e) {
    var $target = $(;
    if (!$"select")) { $(this).hide() };


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.