Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show JQuery UI datepicker on input field click?

OK maybe this is a trivial issue but I'm very interested to see if anyone has any ideas on this:

I am using JQuery UI. I have an <input id="#myfield" type="text"> field and I call $('#myfield').datepicker(). The datepicker shows whenever the field gets focus. But the field is the first one in my form and is already in focus when it loads, so clicking on the field doesn't show it. Also, if I close the datepicker with the Esc key, then I can't open it again by clicking on the field, for the same reason: it's already in focus.

I am aware that I could set the parameter .datepicker({showOn: 'button'}) but I don't want the button. Is there any way to have the datepicker appear when the field gets focus OR is clicked when already in focus? I already tried $('#myfield').click( function () { $(this).focus() } ) and it makes the datepicker open correctly when I click the input field, but then when I select a date it doesn't appear in the field.

like image 293
alexg Avatar asked May 23 '12 10:05

alexg


1 Answers

Try this

<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#date1').datepicker();

$('#date1').focus(function(){
    $('#date1').datepicker('show');
});

$('#date1').click(function(){
    $('#date1').datepicker('show');
});
//$('#ui-datepicker-div').show();
$('#date1').datepicker('show');
});
</script>
</head>
<body>
<input type="text" name="date1" id='date1'>
</body>
</html>
like image 83
Rajan Rawal Avatar answered Oct 23 '22 12:10

Rajan Rawal