How to enter datepicker value in textbox

I have a list of tasks and each task has an edit date option for which I am using jquery datepicker.

This is my application.js application:

$(function() {
   $('.date').datepicker({dateFormat: 'yy-mm-dd'});
});

      

This is my _edit_form.html.erb:

<%= form_for task do |f| %>
  <div class='form-group'>
    <%= f.text_field :description, placeholder: 'create a task' %>
  </div>
  <div class='form-group'>
    <%= f.label :due_date %>
    <%= f.text_field :due_date, placeholder: 'select a date', class:"date", readonly: 'readonly' %>
  </div>
  <%= button_tag(type:'patch', class:"btn btn-default btn-xs") do %>
    <span class='glyphicon glyphicon-plus'></span>
  <% end %>

<% end %>

      

So my problem is the datepicker only works for the first task in my list. I can select a date, it will appear in the textbox, and it will pipe fine to my controller task#update

.

However, every task after that cannot accept input from the datepicker. The textbox is empty when I select a date and it doesn't go to the controller. When I debug the web console, the value is "value" = "".

+3


source to share


1 answer


This is a JQuery issue using datepicker for multiple fields. This is not a Rails fault ...

I think you need the answer " Apply jQuery datepicker to multiple instances : attach unique id

to each field, then script the as function below:

HTML:



<input type="text" class="datepick" id="date_1" />
<input type="text" class="datepick" id="date_2" />
<input type="text" class="datepick" id="date_3" />

      

Script:

$('.datepick').each(function(){
  $(this).datepicker();
});

      

0


source







All Articles