How to close div when div loses focus?


I made a simple plunkr here

I created a little datepicker I would like this to close itself when you focus out of it (focusout of datepicker) if I put blur on input I'm unable to use the datepicker, if I put focusout event on datepicker it doesn't works

I also tried:

angular.element(theCalendar).bind('blur', function () {

but it doesn't work.

Any clue?

this is because you are removing the item before you get a chance to do anything, here is a working example:

just add a timeout:

thisInput.bind('blur', function () {
  }, 200);

have you considered using existing datepickers? like angularUI or angular-strap:


Not a complete solution, but should get you quite closer:

    angular.element($document[0].body).bind('click', function(e){
      var classNamed =  angular.element('class');
      var inThing = (classNamed.indexOf('datepicker-calendar') > -1);
      if (inThing || === "INPUT") {
      } else {
          }, 200);

What you want to do then is to listen for a click on the page, and if the click is outside of the calendar, then close it, otherwise do nothing. The above only takes into account that you are clicking on something that has a class name which includes datepicker-calendar, you will need to adjust it so that clicking within the calendar doesn't close it as well.