0

I have the following javascript

        $(document).ready(function(){
            $.datepicker.setDefaults({
                changeMonth: true,
                changeYear: true,
                dateFormat : 'yy-mm-dd',
                inline : true,
            });
            var dateFields = ['non_fed_photo_id_exp_date', 'non_fed_id_exp_date', 'federal_photo_id_exp_date'];
            $.each(dateFields, function(field){
                $('#UserRegistration_user_'+field).datepicker({
                    yearRange: '<?php echo date('Y'); ?>:+30'
                });
            });
            $('#UserRegistration_user_dob').datepicker({
                defaultDate: '-20y',
                yearRange: '-100:-15'
            });
        });

The UserRegistration_user_dob element is being properly assigned the datepicker but non of the fields in the array are.

Does anything stand out as to what I'm missing / done wrong?

1
  • 1
    try $.each(dateFields, function(index, field) { first param is the index number...or simply add a css class like 'dpicker' and use that as the selector... Commented Mar 25, 2013 at 20:47

1 Answer 1

2

.each()'s callback takes two parameters, the index and value. You're only using the index.

Try this instead:

$.each(dateFields, function (idx,field) {...

Your original code would produce selectors like:

#UserRegistration_user_0 
#UserRegistration_user_1 
#UserRegistration_user_2 

My minor modification would yield:

#UserRegistration_user_non_fed_photo_id_exp_date 
#UserRegistration_user_non_fed_id_exp_date 
#UserRegistration_user_federal_photo_id_exp_date 
Sign up to request clarification or add additional context in comments.

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.