3

I have a button which sets the datepicker to tomorrow using the following code:

$("#txtDateOfJourney").datepicker({ startDate: '+1d'});

But it's not working.

Here's a demo in fiddle

Any Ideas?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
David R
  • 14,711
  • 7
  • 54
  • 72

1 Answers1

6

You only pass in the options (as in datepicker({ startDate: '+1d'})) when you're initializing the controller. Since it's already initialized you don't need to do that. Instead you want to use the setDate() method and then pass in the javascript date representation of tomorrow a string representing the relative date like this:

$('#txtDateOfJourney').datepicker('setDate', "+1d");

Demo in Stack Snippets

$('#btnTomorrow').click(function() {     
  $('#txtDateOfJourney').datepicker('setDate', "+1d");
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script>

<input type="text" id="txtDateOfJourney" class="datepicker" />
<button type="button" id="btnTomorrow" class="btn btn-info">Tomorrow</button>
Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • Thanks KyleMit! Although this works like a charm, I would still want to go with the native way. :-) Any possiblities? – David R Mar 18 '15 at 17:12
  • What do you mean the native way? This is a native method on the bootstrap-datepicker object and plain old javascript. – KyleMit Mar 18 '15 at 17:13
  • When I say native, I mean native to bootstrap's ability, In a nutshell, I want to make " startDate: '+1d' " to work. Hope you understand. – David R Mar 19 '15 at 12:21
  • @DavidR, I updated the answer to parse the string `+1d` as part of the setDate method. Although none of this is provided by bootstrap - they're both handled by the bootstrap-datepicker library. Either way, the exact syntax you're looking for isn't possible, but this will do the same thing. – KyleMit Mar 19 '15 at 12:30