views:

3671

answers:

5

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?

+1  A: 

The obvious answer would be to generate different ids, a separate id for each text box, something like

[int i=0]
<% Using Html.BeginForm()%>
<% For Each item In Model.MyRecords%>
[i++]
<%=Html.TextBox("my_date[i]")%> <br/>
<% Next%>
<% End Using%>

I don't know ASP.net so I just added some general C-like syntax code within square brackets. Translating it to actual ASP.net code shouldn't be a problem.

Then, you have to find a way to generate as many

$('#my_date[i]').datepicker();

as items in your Model.MyRecords. Again, within square brackets is your counter, so your jQuery function would be something like:

<script type="text/javascript">
    $(function() {
        $('#my_date1').datepicker();
        $('#my_date2').datepicker();
        $('#my_date3').datepicker();
        ...
    });
</script>
evilpenguin
Or you could use classes to make the JS simpler...
The Doctor What
Ya, that is not really the obvious answer...
SeanJA
+12  A: 

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)

SeanJA
It looks like the heart of my problem is that the text boxes all have the same ID, so every date picker is updating the first text box. Your example doesn't suffer from that, so it works. How do I get the ID's to be different in a loop?
gfrizzle
Duh, don't let the text boxes have the same ID. Sometimes you have to step away from a problem for a while for the obvious answer to bubble up.
gfrizzle
do damn true /15chars
Konstantinos
A: 

The solution here is to have different IDs as many of you have stated. The problem still lies deeper in datepicker. Please correct me, but doesn't the datepicker have one wrapper ID - "ui-datepicker-div." This is seen on http://jqueryui.com/demos/datepicker/#option-showOptions in the theming.

Is there an option that can change this ID to be a class? I don't want to have to fork this script just for this one obvious fix!!

Alex Grande
That may just be the "sample" part of the sample code. I don't think it actually generates a div with that ID. Making sure each of my target objects had unique ID's made everything work as expected.
gfrizzle
A: 

Date 1:

Date 2:

Date 3:

codeguru