codegeek codegeek - 6 months ago 56
jQuery Question

Bind jquery UI date-picker on focus of input element

Hi i have a situation like i need to bind the jquery UI datepicker({}) function when the input element is focused.i am not able to achieve it with the below code ,so guys plz help how can i bind the datepicker function when the input element is focused



<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<title> Jquery UI Date Picker </title>
</head>
<body>

<input type="text" id="date" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
//$('#date').datepicker({});
$('#date').on('foucs',function(){
$(this).datepicker({});
});
</script>
</body>
</html>




Answer

You can open datepicker using onclick event:

<input type="text" id="date" onclick="$('#date').datepicker();$('#date').datepicker('show');" />

OR

$( "#date" ).focus(function() {
    $('#date').datepicker();
    $('#date').datepicker('show');
});
Comments