1

I need to display different amount of Date Pickers. Forms are generated dynamic, so I can't predict how many Datepickers I will use. Is it possible to use one id for all inputs?

 $("#datepicker").datepicker({ dateFormat: 'yy-mm-dd' }).mask("9999-99-99");
<input type="text" name="startDate1" value="" id="datepicker" >
<input type="text" name="startDate2" value="" id="datepicker" >

http://jsfiddle.net/UKbuC/

1
  • It is not a good practice to use the same id. You can define class which you can use for all the fields, and select them by class name. Commented Mar 27, 2013 at 23:53

2 Answers 2

2

No. You shouldn't have more than element with the same id. In jquery, a better way would be to give them all a class of 'datepicker' and use the selector '.datepicker' instead.

<input type="text" name="startDate1" value="" class="datepicker" >
<input type="text" name="startDate2" value="" class="datepicker" >

$(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' }).mask("9999-99-99");
Sign up to request clarification or add additional context in comments.

Comments

0

Use one class instead of id for all the inputs and bind the datepicker using the class name.

// bind datepicker using class name
$(".mydatepicker").datepicker({ dateFormat: 'yy-mm-dd' }).mask("9999-99-99");

<input type="text" name="startDate1" value="" class="mydatepicker" >
<input type="text" name="startDate2" value="" class="mydatepicker" >

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.