Satch3000 Satch3000 - 6 months ago 51
Javascript Question

jquery datepicker code not working (set specific date as default)

I am trying to set the date to a specific one on page load so I'm trying this code:

<script type="text/javascript">


$(document).ready(function() {

var queryDate = new Date();
queryDate.setFullYear(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});


$("input[type='submit']").click(function(e) {
e.preventDefault();
$.post("s.php", $("form").serializeArray(), function(message) {
window.location="v.php"
});
});
});

</script>

//The form part where is displays the datepicker:

<form action="#" method="POST">
<div data-role="fieldcontain">
<label for="date">Date:</label>
<input type="date" name="date" id="date" value="" />
<input type="submit" value="submit" />
</div>
</form>


The problem is that nothing's changing ... the default is still the current date :o/

Any ideas anyone please?

UPDATE: Tried this code:

var queryDate = new Date(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});
$('#date').val(queryDate);


And the date is appearing in the input box but the calendar is not in the right month nor date ... Moving foward but still not working.

Answer

Umm what if you tried this

new Date(year, month, day, hours, minutes, seconds, milliseconds)

var queryDate = new Date();
queryDate.setFullYear(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});

Then the datepicker knows its a date object and how to handle it.


EDIT Ok - all i did was copy the code exactly as you got it here several posts the same suggestion- and the code you edited. Added tags- inlcuded jquery and jquery ui.. I see no problem- the defuatl date is 2009.

</head>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery-ui.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() { 
var queryDate = new Date();
queryDate.setFullYear(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});

$("input[type='submit']").click(function(e) {
        e.preventDefault();
        $.post("s.php", $("form").serializeArray(), function(message) {
            window.location="v.php" 
        });
      });
   });
  </script>

</head>

<body>
//The form part where is displays the datepicker:

<form action="#" method="POST">
<div data-role="fieldcontain">
<label for="date">Date:</label>
<input type="date" name="date" id="date" value=""  />
<input type="submit" value="submit" />

</div>     
</form>

</body>

Result (no styles sheet attached) enter image description here

Working example- tell me what is wrong

http://jsfiddle.net/ppumkin/nptgn/

Comments