Topic: Datepicker - Event does not trigger on fresh page load

fkmccallion free asked 3 years ago

Expected behavior Selecting the calendar icon in the datepicker triggers the calendar to pop up.

Actual behavior The calendar pops up when I navigate from one page to the page where the date picker exists. On the page where the date picker exists if it is refreshed when the calendar is clicked the popup does not open.

Resources (screenshots, code snippets etc.)

$('.datepicker').datepicker({ min: true, format: 'yyyy/mm/dd' });
  <%= form_tag events_path, method: :get do %>
    <div class="row no-gutters">
      <div class="col">
        <div class="md-form md-outline input-with-post-icon datepicker mt-1">
          <input placeholder="Select Date" type="text" id="selected_date" name="selected_date" class="form-control">
          <i class="fas fa-calendar input-prefix" tabindex=1></i>
      <div class="col-auto">
        <button type="submit" class="btn btn-primary btn-rounded m-0 align-bottom"><i class="far fa-hand-pointer"></i></button>
  <% end %>

Grzegorz Bujański staff commented 3 years ago

Hi. Are you sure you have correctly connected MDB on this page? You can check in dev tools if the icon has an event on click?

fkmccallion free commented 3 years ago

I just figured out the issue - this is specifically a RoR issue I've ran into before. It is related to turbolinks and to get the functionality to work you will need to wrap the jQuery in an eventlistener as such:

document.addEventListener("turbolinks:load", function(event) { $('.datepicker').datepicker({ min: true, format: 'yyyy/mm/dd' }); // add listener for date change $('.datepicker').on('change', submitForm); function submitForm() { $('#form-submit').submit() } });

Please insert min. 20 characters.


Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.



Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: 4.19.1
  • Device: mac pro
  • Browser: chrome
  • OS: Mojave
  • Provided sample code: No
  • Provided link: No