1

At the moment in my JS I statically define which input ID's I want to associate my datepicker script with.

Like so:

$('datepicker1').glDatePicker();

However to make this dynamic I have refactored this into a for loop so that for multiple fields that need a datepicker I can call them in that loop.

dateArray[y] =  "datePicker" + i;
//out of loop scope

for (i = 0; i < dateArray.length; i++)
    {
        $(dateArray[i]).glDatePicker();

    }

It does not seem to work , I have checked that there are values in the array by printing the values to the log.

Is this a limitation of jQuery?

Thanks

2
  • 1
    Is datepicker1 the tag name ? Commented Feb 8, 2013 at 10:36
  • You can avoid loops, and make a simple single call (see my answer). Commented Feb 8, 2013 at 10:58

3 Answers 3

3

You need to set it as an id with "#"

 $("#" + dateArray[i]).glDatePicker();
Sign up to request clarification or add additional context in comments.

1 Comment

THanks, cant believe I missed that
1

which input ID's I want to associate my datepicker script with.

i think you missed the id selector

$('#'+ dateArray[i]).glDatePicker();
---^---here

Comments

1

It is better to make a single jQuery call to glDatePicker(), by concatenating all ids into a string

var allIds = '';
for (i = 0; i < dateArray.length; i++) {
  allIds += '#' + dateArray[i] + ',';
}
$(allIds).glDatePicker();

Or even better, put a class to all those fields, and simply call

$(".datepicker").glDatePicker();

There is still another single call solution (assuming all your ids start with "datePicker"):

$('[id^="datePicker"]').glDatePicker(); // selects all elements that have id starting with datePicker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.