Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply jQuery datepicker to multiple instances

I've got a jQuery date picker control that works fine for once instance, but I'm not sure how to get it to work for multiple instances.

<script type="text/javascript">     $(function() {         $('#my_date').datepicker();     }); </script>  <% Using Html.BeginForm()%> <% For Each item In Model.MyRecords%> <%=Html.TextBox("my_date")%> <br/> <% Next%> <% End Using%> 

Without the For Each loop, it works fine, but if there's more than one item in the "MyRecords" collection, then only the first text box gets a date picker (which makes sense since it's tied to the ID). I tried assigning a class to the text box and specifying:

$('.my_class').datepicker(); 

but while that shows a date picker everywhere, they all update the first text box.

What is the right way to make this work?

like image 946
gfrizzle Avatar asked Apr 01 '09 22:04

gfrizzle


People also ask

How do I use datepicker?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.


2 Answers

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(); }); 

(pseudo coded up a bit to keep it simpler)

like image 86
SeanJA Avatar answered Oct 19 '22 02:10

SeanJA


I just had the same problem.

The correct way to use date pick is $('.my_class').datepicker(); but you need to make sure you don't assign the same ID to multiple datepickers.

like image 37
Shuo Avatar answered Oct 19 '22 00:10

Shuo