0

I have a situation where when I click on milestones link it unhide a dropdown div with milestones as a heading.

My requirement is when I click inside that div anywhere it should not get hide. it should get hide when I click outside that div only. Current problem is when I click inside that div it get hide. Here is the link to the image just for reference. https://drive.google.com/file/d/0B0zHBJb86qsIUFpWMm1ra3dfc1k/view

$("#headermile").on({//task list in header
  focus:function(){
      $('#mileheader').attr("class", "");
  }, 
  blur:function(){    

    $('#mileheader').click(function (e) {
      if (e.target.id == 'mileheader') {
        $("#mileheader").attr("class","");
      } else {
        $("#mileheader").attr("class","hide");
      }

  });
<li class="has-dropdown">
  <a id="headermile" href="#">Milestones </a>
  <div id="mileheader" class="hide">
    <ul class="dropdown settings">
    </ul>
  </div>
</li>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vishnuk
  • 79
  • 9

1 Answers1

0

Don't think this answer is perfect, just wanted to find a quick solution.

http://jsfiddle.net/troythompson/eLz7ptwf/

jQuery(document).ready(function($) {

    $('#headermile').click(function(event) {
        event.preventDefault();
        $("#mileheader").removeClass("hide");
    });

    $(document).on('click', function(event) {
        var container = $("#mileheader");
        var button = $("#headermile");
        if (!container.is(event.target) && container.has(event.target).length === 0 && !button.is(event.target)) {
            $("#mileheader").addClass("hide");
        }
    });
});

Resource: Use jQuery to hide a DIV when the user clicks outside of it

Again, I don't use .attr to try to remove a single class, as it will remove all classes on that element.

Community
  • 1
  • 1
Troy Thompson
  • 371
  • 1
  • 6