Datepicker opening when date set

Datepicker opening when date set

I have a number of datepickers attached to text boxes. Then I have some buttons which when clicked set the dates on these datepickers to some presets. I use the setDate method like so when a button is clicked:

  1. $('#selector1').datepicker('setDate', new Date(year, month, day));
  2. $('#selector2').datepicker('setDate', new Date(year, month, day));

This works fine. However, if you click on the text box and choose a date yourself with the datepicker, then whichever datepicker you last used pops open and receives focus when the setDate is called. I don't want that to happen.

It does not happen if you have never used the datepickers (e.g. on a new page load). And it only happens to the last datepicker you used.

How can I stop it?