1

I have a calendar that does not close on date select. So I have targeted a class(.day) to close the calendar when clicked. However, when I reopen the calendar and select a new date it does not close a second time. I have looked for answers on here and although similar titles and issues the code isn't quite the same...

$('.day').click(function () {
  $(".datepicker").hide();
});

<span class="input-append date dp-ticket" data-date="today" data-date-format="dd-mm-yyyy">
  <input type="text" class="add-on form-control col-xs-10 col-sm-10 col-md-10 col-lg-10" placeholder="Or, choose any date" name="startdate" id="startdate">
  <button class="add-on btn btn-search col-xs-2 col-sm-2 col-md-2 col-lg-2" type="button">
    <span class="icon-calendar"></span>
  </button>
</span>
Beat Richartz
  • 9,474
  • 1
  • 33
  • 50
Tom Rudge
  • 3,188
  • 8
  • 52
  • 94

3 Answers3

0

Try using on.

$('.day').on('click', function () {
  $(".datepicker").hide();
});
Neel
  • 220
  • 1
  • 10
0

Sorry but I'm not seeing .day or .datepicker in the code sample other than in the jQuery. Maybe I'm missing something.... lol

My guess is that the script registers the original hide event but does not register that .datepicker is revealed again when you try and change the date, so as far as the script knows it's still hidden when you change the date.

You could try putting an .change() to detect a change in the value of the date field(or wherever your placing the date data.)

$(".foo").val().change(function() {
  $(".datepicker").hide();
});

You may need to set a default value, but every time it's changed it will run the function to hide .datepicker.

m59
  • 43,214
  • 14
  • 119
  • 136
Ant
  • 42
  • 2
  • 8
0

I'm not sure how .day is being implimented, as you do not show it in your code, but because it only fires once, I am thinking that you are adding .day to the DOM dynamically.

If that is the case, you can do this:

$('SELECTOR_OF_PARENT').on('click', '.day', function(){
    $(".datepicker").hide();
});

You may also do it like this

$(document).on('click', '.day', function(){
    $(".datepicker").hide();
});

Here is more information for .on()

Shawn G.
  • 622
  • 4
  • 16