jQuery Datepicker for multiple inputs

Gusgus picture Gusgus · May 29, 2012 · Viewed 44.7k times · Source

I have a jQuery datepicker script:

$(function() {
    $( "#datepicker" ).datepicker({ dateFormat: "yyyy-mm-dd" });
}); 

When I want to initialize this script for two inputs, it works only for the first one. How to use it for both inputs?

<input type="text" name="MyDate1" id="datepicker">
<input type="text" name="MyDate2" id="datepicker">

Answer

thecodeparadox picture thecodeparadox · May 29, 2012

Just change all id to class.

<input type="text" name="MyDate1" class="datepicker">
<input type="text" name="MyDate2" class="datepicker">

$(function() {
  $( ".datepicker" ).datepicker({ dateFormat: "yyyy-mm-dd" });
}); 

also can use

$(function() {
  $( "input[name^=MyDate]" ).datepicker({ dateFormat: "yyyy-mm-dd" });
});