views:

3509

answers:

2

Thanks for taking the time to read this.

I have an unknown number of (dynamically inserted) input elements in a form, to each of which I want to attach the datepicker widget included in jQuery UI.

All of the input elements that I want to attach the datepicker to have a class of "date-pick". Obviously because we have more than one matching element we need more than just a class name so that the date value gets returned to the originating field rather than just the first match.

Ordinarily I'd probably just do something like:

        $("input.date-pick").each(function(){
     $(this).datepicker({dateFormat: "yy-mm-dd"});
    });

However in the code I'm working with, the input elements do not have unique IDs until the form data is saved (something that I'm unable to change), though they do have unique name attribute values of the format name ="field_id_x[y][z]". which, when the form is saved then gets converted to an id of the format id="field_id_xyz".

So my question is, can anyone show me how to loop through all the input elements with a class of "date-pick" based on their name attribute values?

Thanks again for your time and attention.

(PS It might also be worth mentioning that the number of matching input elements in the form can be increased/decreased by the user on the fly.)

+1  A: 

The date picker doesn't need the elements to have an ID. You're already looking up the elements for it. This should work:

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

Note you don't even need the each call.

Complete example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>DatePicker Test Page</title>
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.datepicker.js"&gt;&lt;/script&gt;
<script type='text/javascript'>
$(function() {
    $('.dp').datepicker();
});
</script>
</head>
<body>
<hr />
<input class='dp' type='text'>
<input class='dp' type='text'>
<input class='dp' type='text'>
</body>
</html>
T.J. Crowder
Thanks for your answer.I did try that alredy, the problem is that no matter what field the datepicker is triggered by, the date value gets entered into the first matching input element that jquery finds.For example using the code in your answer, if I click the third input the datepicker is triggered but when a date is selected it is inserted into the first input element in the html rather than the third...
meta
@meta: That's strange, it doesn't do that for me, the date goes in the correct fields when I run that test page. I've tried IE7, FF3.0 (haven't upgraded yet), Safari 3 (erm, ditto), Opera 9.64, and Chrome 2. I think it must be something else on your page causing that behavior...
T.J. Crowder
...and FF 3.5.2 and Safari 4.0.3 (just for completeness).
T.J. Crowder
hmmm okay, thanks T.J. - you're completely right as your test page works as expected.Think I've spotted the issue which is that the dynamically inserted input elements all have the same id value - leading to the "first element" behaviour I encountered. unfortunately that's not something I'm able to change in the environment I'm working in.sorry, I hadn't seen that before now.Guess I''ll either take it up with the developer who's code I'm hooking into or try another route.thanks again for your time though
meta
@meta: Good luck with it. You definitely want to sort that out, having multiple elements with the same `id` is a Bad Thing.
T.J. Crowder
+1  A: 

Your code should work as it is. However, you could loop through by name like this:

$("input[name*='field_id_'].date-pick").each(function(){
   $(this).datepicker({dateFormat: "yy-mm-dd"});
});
kaiz.net
cheers for your answer - I should have mentioned though that I have other input elements in the same form with name attributes starting with `field_id_` so need to loop through based on both name and class.
meta
I added the class to the selector.
kaiz.net
thanks kaiz.net, but that code only seems to trigger when the first (in html order) input element is clicked and not subsequent ones... am going to try an even more stripped down test case without the dynamically inserted inputs and see if it works then
meta
@kaiz.net, your solution works perfectly with a minimal test case but is broken by the duplicate id values inserted by the code I'm working with - thanks for your time and attention though!
meta