12

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">
3
  • have a class to all the inputs which need a datepicker Commented May 29, 2012 at 17:00
  • $('.your-class').datepicker({ dateFormat: "yyyy-mm-dd" }); Commented May 29, 2012 at 17:00
  • in the first place, you can't have more than one id Commented May 29, 2012 at 17:03

9 Answers 9

29

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" });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I read through several threads on this that suggested super complicated ways to achieve this - yours took me about .5 seconds and worked like a charm. Thanks!
dateFormat: "yyyy-mm-dd" will display: YYYYYYYY-MM-DD remove the extra yy to display correct format. Update to: dateFormat: "yy-mm-dd"
An id may only be used once on a page, and clearly jQuery adheres to that spec.
it's helped me! THX
6

Tired of repeating this, but still, am repeating it once more. ids have to be unique. So, use this:

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

with

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

Comments

4

It isn't valid to have duplicated IDs. You should add a class instead:

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

Then you can do:

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

Comments

3

Access datepicker function by Name:

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

Comments

1

You could also add classes on the fly if input fields are dynamically generated, such as in Django templates.

<input type="text" name="MyDate1" id="datepicker1">
<input type="text" name="MyDate2" id="datepicker2">

Get input fields using #id and add a class

$(function() {
    $( "#datepicker1, #datepicker2" ).addClass('datepicker');
    $( ".datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });
});

Comments

0
<input type="text" name="MyDate1" id="datepicker1">
<input type="text" name="MyDate2" id="datepicker2">

Just get input fields using #id and apply datepicker.

$(function() {
    $( "#datepicker1, #datepicker2" ).datepicker({ dateFormat: "yy-mm-dd" });
});

Comments

0

Access date picker by id

<input type="text" name="MyDate1" id="datepicker1">
<input type="text" name="MyDate2" id="datepicker2">

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

OR access date picker by name:

<input type="text" name="MyDate1" id="datepicker1">
<input type="text" name="MyDate2" id="datepicker2">

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

Comments

0

I wrote code like this and work properly in multiple field form:

Add this code before loop:

<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css"/>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>

<script>
  $('.mydatepicker').each(function(){
    $(this).datepicker();
  });
</script>

And this line is in loop:

<div class="control-group form-group" style="padding: 5px;">
  <div class="controls">
    <span class="bi bi-calendar2-minus" style="color: #FF6600;"></span>
    Input Date
    <input name="myDate$i" type="text" class="form-control mydatepicker" id="myDate$i" required ="true" placeholder="Contoh: 2022-11-25" data-bs-toggle="tooltip" style="border-radius: 3px; padding:10px;"/>
  </div>      
</div>

Comments

-2
  <script>
  $(document).ready(function() {
    $("#datepicker").datepicker();
  });
  $(document).ready(function() {
    $("#datepicker2").datepicker();
  });
  $(document).ready(function() {
    $("#datepicker3").datepicker();
  });
  </script>

And just give the forms the id's: datepicker datepicker2 datepicker3

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.