admin管理员组

文章数量:1201647

I've explored maybe 30 threads and my problem still isn't solved.

I have a JQuery datepicker:

<input id="datepicker" name="date" class="mydatepicker" onchange="isValidDate()"></input>

I simply want to write a javascript function that accomplishes this when called:

<script type="text/javascript">
document.getElementById("datepicker").value = 'certain date';
</script>

datepicker is tied to these files:

<link href=".8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src=".6.2/jquery.min.js"></script>
<script src=".8/jquery-ui.min.js"></script>

I've explored maybe 30 threads and my problem still isn't solved.

I have a JQuery datepicker:

<input id="datepicker" name="date" class="mydatepicker" onchange="isValidDate()"></input>

I simply want to write a javascript function that accomplishes this when called:

<script type="text/javascript">
document.getElementById("datepicker").value = 'certain date';
</script>

datepicker is tied to these files:

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
Share Improve this question asked Jul 3, 2015 at 8:29 bobbob 2352 gold badges3 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

You need to use the setDate function of the datepicker library. Try this:

$('#datepicker').datepicker('setDate', new Date()); // = set to today

As this is the first post that pops up when you search for how to set date in a datepicker, here's an answer for everyone that comes here trying to figure out how to do it with vanilla JS:

const date = document.getElementById('datepicker');
date.value = new Date().toISOString().split('T')[0].slice(0, 10);
<input id="datepicker" name="date" class="mydatepicker" />

本文标签: javascriptSet value of datepickerStack Overflow