JQuery - datepicker doesn't appear - javascript

I am new in JQuery and I need to use datepicker to get date in form. I am using this code:
HTML
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#date" ).focus().datepicker({ dateFormat: 'dd-mm-yy' });
});
</script>
</head>
<body>
<p>Date: <input type="text" id="date"></p>
</body>
</html>
But when I click on field, nothing happen. When I tried to use this:
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<div id="datepicker"></div>
<script>
$( "#datepicker" ).datepicker();
</script>
</body>
</html>
It is working... Thanks for tips.
Additionally: I am using Nette framework, where I can inherit templates... Could this be the problem?

Just focus it after you've initialized the datepicker, otherwise the input is focused before there is a datepicker attached.
$( "#date" ).datepicker({ dateFormat: 'dd-mm-yy' }).focus();
FIDDLE

Try initializing with the option like this:
$('#date').datepicker( "option", "dateFormat", 'dd-mm-yy' ).datepicker();

Related

jQuery date-picker javascript syntax problem

the errortrying to do something simple but I got an error, I`m a beginner so I have a lot of syntax problems.
thank you!
<input class="DateFrom" placeholder="min 7 days">
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.9.1.js"
integrity="sha256-e9gNBsAcA0DBuRWbm0oZfbiCyhjLrI6bmqAl5o+ZjUA="
crossorigin="anonymous"></script>
<script type="text/javascript"
src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"
integrity="sha256-PsB+5ZEsBlDx9Fi/GXc1bZmC7wEQzZK4bM/VwNm1L6c="
crossorigin="anonymous"></script>
<script type="text/javascript">
$("input.DateFrom").datepicker({
minDate: 7,
dateFormat: 'dd/mm/y',//check change
changeMonth: true,
changeYear: true
});
</script>
</body>
Try this,
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "input.DateFrom" ).datepicker( {
minDate: 7,
dateFormat: 'dd/mm/y',
changeMonth: true,
changeYear: true
});
} );
</script>
</head>
<body>
<p>Date: <input type="text" class="DateFrom"></p>
</body>
</html>

How to show date picker value in a label and textbox

$("#datepicker").datepicker();
I am using this function
and its box of input is
<input type="text" id="datepicker" name="fulldate" class="form-control wid-100"> <span id="#dpicker_name"></span>
Please help me in it friends
You have to do like
$(document).ready(function(){
$("#datepicker").datepicker({
onSelect: function(dateText, inst) {
$("#dpicker_name").html(dateText);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css">
<input type="text" id="datepicker" name="fulldate" class="form-control wid-100">
<span id="dpicker_name"></span>
Please check Documentation
Also you can get it without onSelect like
$("#datepicker").datepicker("getDate");
Try this
$( "#datepicker" ).datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date"
});
Seems to me you want to trigger the datepicker on click of icon. Check the docs here .
used to this code
Demo
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
$(document).ready(function(){
$('#datepicker').on('change', function(){
var dateThis = $(this).val();
$('#dpicker_name').text(dateThis);
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"> <span id="dpicker_name"></span></p>
</body>
</html>
Try this.
$("#dpicker_name").text($( "#datepicker" ).datepicker( "getDate" ));
Get value in javascript as
var date = $(".datepicker").val();
and assigned it to the lable

Date Picker Options not working

I tried every options. MaxDate, MinDate, Beforeshowday. But its still not working. But my Datepicker is showing and working well. But I cant configure any options in my date picker. Any help would be greatly appreciated
Here is the code below
<script type="text/javascript">
jQuery(document).ready(function(){
$('#datepicker').datepicker({
beforeShowDay: $.datepicker.noWeekends,
minDate: '0',
});
});
</script>
Please checkout the code sample from https://jqueryui.com/datepicker/#default
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Dates in other months</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker({
showOtherMonths: true,
selectOtherMonths: true
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
And for beforeShowDay, Please check JQuery DatePicker and beforeShowDay

How to disable the date (tomorrow date) in jquery

How to disable the next date. I can select the previous date but not the next coming dates. Can any one help me? This is the code I am using.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
You can disable selectability of future dates by specifying 0 for the maxDate option.
Documentation
$( "#datepicker" ).datepicker({
maxDate: 0 // 1 for tomorrow, 2 for day-after, and so on..
});
This will work:
$("#datepicker").datepicker({dateFormat: 'yy-MM-dd',maxDate: 0 });

datepicker doesn't work after load page using jquery

here is the my code,in my home page there is a button to load this page, it is done by using jquery $("#content").load(site_url); like this. but this date picker doesn't work
</script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: "dd-mm-yy" });
});
</script>
<div id="content">
<form>
<input type="text" id="datepicker">
</form>
</div>

Categories