0

In my application, i have calendar icon so when it is clicked the calendar pop up shows.

So, I have the following jQuery code which hides the section when i clicked anywhere of <Div> matching with css class.

<script>
$(document).ready(function(){
    $(".myContent").click(function () {
        $(".calendar").hide();
    });
});
</script>

However, the issue i am facing is it works fine for the first time

i.e. Click on Calendar Icon shows calendar popup.. than click on anywhere inside Div hides calendar pop-up.

However, When i reclick on Calendar icon than it does not show calendar popup unless i refresh the whole page.

I am guessing that i need to unbind click event but not sure how... Any help please?

TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81

5 Answers5

2

Use event.stopPropagation to keep the click on the calendar icon from bubbling out to .myContent:

$("#calendar_icon").click(function(e) {
    e.stopPropagation();
    $(".calendar").show();
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

To unbind You can use off like:

$(".myContent").off('click');

Please refer: jQuery Documentation for more details.

madoxdev
  • 3,770
  • 1
  • 24
  • 39
0

You can use off.

var handler = function(){ /* whatever */ };
$(selector).on('click', handler);
$(selector).off('click', handler);

Alternatively if you only want it to happen at most one time...

$(selector).one('click', function(){ /* whatever */});
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

Try:

<script>
$(document).ready(function(){
    $('body').on('click', '.myContent', function() {
        $(".calendar").hide();
    });
});
</script>
Alliswell
  • 1,523
  • 20
  • 35
0

Not sure about your angularjs but try this:

$(document).ready(function(){
     $(document).on("click", ".myContent, function () {
           $(".calendar").hide();
     });

     $("#CalendarSettings").on("click", function()  {
           $(".calendar").show();
     });
  });
brroshan
  • 1,640
  • 17
  • 19