-2

I would like to use the same JavaScript function for two or more controls.

Is this possible and how?

this is my example:

<script>
  $(function () {
      $("#datepicker").datepicker({
          showOtherMonths: true,
          selectOtherMonths: true
      });
  });

<input type="text" id="datepicker">
<input type="text" id="datepicker2">
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Erbez
  • 321
  • 5
  • 17

4 Answers4

1

Change your selector to the Class Selector so all the equal classes get affected by the function

<script>
  $(function () {
      $(".datepicker").datepicker({
          showOtherMonths: true,
          selectOtherMonths: true
      });
  });

<input type="text" class="datepicker">
<input type="text" class="datepicker">
1

Yes you can use a multiple id selector:

$("#datepicker, #datepicker2").datepicker({
      showOtherMonths: true,
      selectOtherMonths: true
});

Or add a classe and use a selector by class.

Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
1

You need to add id of all the elements you need to add datepicker to, just like below:

$("#datepicker,#datepicker2").datepicker({
   showOtherMonths: true,
   selectOtherMonths: true
});
Arpit Arya
  • 123
  • 6
0

Use a class selector instead of id selector and things should be good.

<script>
$(function () {
  $(".selectorname").datepicker({
      showOtherMonths: true,
      selectOtherMonths: true
   });
});

<input type="text" class="selectorname">
<input type="text" class="selectorname">
Cleaven
  • 974
  • 2
  • 9
  • 30