How can I trigger onSelect event when I select a selected date on jQuery UI datepicker?
Working demo : http://jsfiddle.net/YbLnj/
Documentation: http://jqueryui.com/demos/datepicker/
code
$("#dt").datepicker({
onSelect: function(dateText, inst) {
var date = $(this).val();
var time = $('#time').val();
alert('on select triggered');
$("#start").val(date + time.toString(' HH:mm').toString());
}
});
Use the following code:
$(document).ready(function() {
$('.date-pick').datepicker( {
onSelect: function(date) {
alert(date)
},
selectWeek: true,
inline: true,
startDate: '01/01/2000',
firstDay: 1,
});
});
You can adjust the parameters yourself :-)
If you are also interested in the case where the user closes the date selection dialog without selecting a date (in my case choosing no date also has meaning) you can bind to the onClose event:
$('#datePickerElement').datepicker({
onClose: function (dateText, inst) {
//you will get here once the user is done "choosing" - in the dateText you will have
//the new date or "" if no date has been selected
});
$(".datepicker").datepicker().on("changeDate", function(e) {
console.log("Date changed: ", e.date);
});
If the datepicker is in a row of a grid, try something like
editoptions : {
dataInit : function (e) {
$(e).datepicker({
onSelect : function (ev) {
// here your code
}
});
}
}
Related
I am new to JS. I have added JS function to prevent user to insert invalid(like 99/99/0000) date manually in datepicker. I have written all the function and it is working as expected but somehow when you entered the invalid date it throws the error but that alert box is not closed it remains as it is. Please guide me here Thanks in advance.
Function :
$(function() {
var dateFormat = "mm/dd/yy";
var beginDate = $("#dobEdit").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true
})
.on({
change: function() {
beginDate.datepicker("option", "minDate", getDate(this));
},
blur: function() {
beginDate.datepicker("option", "minDate", getDate(this));
}
});
function getDate(element) {
var date;
try {
date = $.datepicker.parseDate(dateFormat, element.value);
} catch (error) {
alert(error);
}
return date;
}
});
I'm working at a checkout page. I need to disable an input (#dreturn) if the previous input (dpick) is empty.
This is my code:
$(document).ready(function(){
var minDate = new Date();
$("#dpick").datepicker({
showAnim: 'drop',
numberOfMonth: 1,
maxDate: 7,
minDate: minDate,
dateFormat: 'yy/mm/dd',
onClose : function(selectedDate){
$('#dreturn').datepicker("option", "minDate", selectedDate);
}});
var minDate = new Date();
$("#dreturn").datepicker({
showAnim: 'drop',
numberOfMonth: 1,
maxDate: 7,
minDate: minDate,
dateFormat: 'yy/mm/dd',
onClose : function(selectedDate){
$('#dreturn').datepicker("option", "minDate", selectedDate);
}});
This is my datapicker but this isn't helping me.
$('#dreturn').change(function() {
var start = $('#dpick').datepicker('getDate');
var end = $('#dreturn').datepicker('getDate');
if (start<end) {
var days = (end - start)/1000/60/60/24;
$('#days').val(days);
}
else {
alert ("Please chech again your dates!");
$('#dpick').val("");
$('#dreturn').val("");
$('#days').val("");
}
});
I've tried this but isn't working.
if (!'dpick').val() {
document.getElementById("dreturn").disabled = true;
}
});
Change your if statement to
if(!Date.parse($("dpick").val())){
and move this if to blur event handler, something like this
$("#dpick").blur(function (){
if(!Date.parse($(this).val())){
document.getElementById("dreturn").disabled = true;
}else{
document.getElementById("dreturn").disabled= false;
}
})
Initially disable the second date-picker "#dreturn" after initializing your date-pickers:
$(document).ready(function(){
//initialization
$("#dpick").datepicker({...});
$("#dreturn").datepicker({...});
$("#dreturn").prop('disabled',true);
/****************** new code to be inserted see below ************/
});
Now, on the change of "#dpick" enable "#dreturn":
/************************ CODE TO BE INSERTED ABOVE ****************/
$("#dpick").change"(
function()
{
var val= $("#dpick").val();
if(!(val))
{
$("#dreturn").prop('disabled',false);
}
}
);
Note:
1. " if(val)" evaluates to true if "val" is not null, undefined and the jQuery UI documentation states
for the datepicker in https://api.jqueryui.com/datepicker/#option-defaultDate that the default value is "null".
2. Now to use "change()" for the datepicker of jQuery UI, check jQuery Datepicker onchange event issue which
so, it means you have to use "onSelect" of jQuery datepicker UI to address the defect of your datepicker.
$(document).ready(
function()
{
//initialization
$("#dpick").datepicker({
showAnim: 'drop',
numberOfMonth: 1,
maxDate: 7,
minDate: minDate,
dateFormat: 'yy/mm/dd',
onClose : function(selectedDate){
$('#dreturn').datepicker("option", "minDate", selectedDate);
},
onSelect: function(){
$(this).change();
}
});
$("#dreturn").datepicker({.....});
$("#dreturn").prop('disabled',true);
$("#dpick").change"(
function()
{
var val= $("#dpick").val();
if(val)
{
$("#dreturn").prop('disabled',false);
}
}
);
}
);
And your problem should be solved. Hope it helps. :-)
Working on clone using datepicker. I have searched in stackoverflow for my issue i am not getting the correct stuff. When the user clicks the date from the original date it was working as expected But once the user click the addmore button in the cloned div the datepicker was not working. I tried giving .destroy the result was not coming as expected.It might be the duplicate question but as I said the solution not working in my case.
Here is the jquery code.
var currentDate = new Date();
$(".cloned-row1").find(".deg_date").removeClass('hasDatepicker').datepicker({
dateFormat: "mm-dd-yy",
changeMonth: true,
yearRange: "-100:+0",
changeYear: true,
maxDate: new Date(),
showButtonPanel: false,
beforeShow: function () {
setTimeout(function (){
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
});
$(".deg_date").datepicker("setDate", currentDate);
var count=0;
$(document).on("click", ".edu_add_button", function () {
alert("checj");
var $clone = $('.cloned-row1:eq(0)').clone(true,true);
//alert("Clone number" + clone);
$clone.find('[id]').each(function(){this.id+='someotherpart'});
$clone.find('.btn_more').after("<input type='button' class='btn_less1' id='buttonless'/>")
$clone.attr('id', "added"+(++count));
$clone.find(".school_Name").attr('disabled', true).val('');
$clone.find(".degree_Description").attr('disabled', true).val('');
$clone.find("input.deg_date").datepicker();
$(this).parents('.educat_info').after($clone);
});
$(document).on('click', ".btn_less1", function (){
var len = $('.cloned-row1').length;
if(len>1){
$(this).closest(".btn_less1").parent().parent().parent().remove();
}
});
Here is the fiddlelink
Thanks in advance
Jquery datepicker creates UUID-based ID attributes for the input fields it binds when you initialize it. You cloning those elements results in more elements with either the same ID (which jQuery does not like) or a different ID if your clone routine manages that (which means datepicker does not know about the clones).
try updating your js code like this
$clone.find("input.deg_date")
.removeClass('hasDatepicker')
.removeData('datepicker')
.unbind()
.datepicker({
dateFormat: "mm-dd-yy",
changeMonth: true,
yearRange: "-100:+0",
changeYear: true,
maxDate: new Date(),
showButtonPanel: false,
beforeShow: function() {
setTimeout(function() {
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
});
here's the updated JSFIDDLE. hope this helps.
You can re-initiate datepicker,
Put datepicker line at last on click event
$(document).on("click", ".edu_add_button", function () {
...
...
...
...
$(".deg_date").datepicker("setDate", currentDate);
});
It will work!!!
I am prototyping an app in Meteor and am having trouble using the datepicker. I created a minimal example in Meteorpad.
How can I get the selected value from the datepicker when clicked and save to the db? My code for the event is in line 8 of client/app/js. I'm using an input tag as part of the datepicker but since there is no submit button, I'm not sure if I am actually changing the date. Does onchange work here?
Template.Valuation.events({
'onchange #selectedDate': function(e) {
e.preventDefault();
var valuationId = this._id;
var selectedDate = $(e.target).find('[name=selectedDate]').val();
Valuations.update(valuationId, {$set: {valuationDate: selectedDate}}, function () {
});
}
});
Does onchange work here?
No, it does not. You will need to listen for changeDate events in your Template.Valuation.onRendered function:
Template.Valuation.onRendered(function() {
$('#valuation .input-group.date').datepicker({
todayBtn: "linked",
orientation: "top auto",
daysOfWeekDisabled: "0,6",
todayHighlight: true,
autoclose: true,
format: 'yyyy-mm-dd'
});
var self = this;
$('#valuation .input-group.date').datepicker().on('changeDate', function(ev) {
var valuationId = self.data._id;
var selectedDate = $('#selectedDate').val();
Valuations.update(valuationId, {
$set: {
valuationDate: selectedDate
}
}, function() {});
});
});
I found also a bug in your valuationPrice template helper. This may fix it:
valuationPrice: function() {
var valuation = Valuations.findOne({
_id: this._id
});
var valuationDate = valuation.valuationDate;
var valuationPrice = 0;
_.each(valuation.closingPrices, function(closingPrice) {
if (closingPrice.date == valuationDate) valuationPrice = closingPrice.close;
});
return valuationPrice;
}
Here is the updated MeteorPad.
Working on clone using datepicker. I have searched in stackoverflow for my issue i am not getting the correct stuff. When the user clicks the date from the original date it was working as expected But once the user click the addmore button in the cloned div the datepicker was not working. I tried giving .destroy the result was not coming as expected.It might be the duplicate question but as I said the solution not working in my case.
Here is the jquery code.
var currentDate = new Date();
$(".cloned-row1").find(".deg_date").removeClass('hasDatepicker').datepicker({
dateFormat: "mm-dd-yy",
changeMonth: true,
yearRange: "-100:+0",
changeYear: true,
maxDate: new Date(),
showButtonPanel: false,
beforeShow: function () {
setTimeout(function (){
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
});
$(".deg_date").datepicker("setDate", currentDate);
var count=0;
$(document).on("click", ".edu_add_button", function () {
alert("checj");
var $clone = $('.cloned-row1:eq(0)').clone(true,true);
//alert("Clone number" + clone);
$clone.find('[id]').each(function(){this.id+='someotherpart'});
$clone.find('.btn_more').after("<input type='button' class='btn_less1' id='buttonless'/>")
$clone.attr('id', "added"+(++count));
$clone.find(".school_Name").attr('disabled', true).val('');
$clone.find(".degree_Description").attr('disabled', true).val('');
$clone.find("input.deg_date").datepicker();
$(this).parents('.educat_info').after($clone);
});
$(document).on('click', ".btn_less1", function (){
var len = $('.cloned-row1').length;
if(len>1){
$(this).closest(".btn_less1").parent().parent().parent().remove();
}
});
Here is the fiddlelink
Thanks in advance
Jquery datepicker creates UUID-based ID attributes for the input fields it binds when you initialize it. You cloning those elements results in more elements with either the same ID (which jQuery does not like) or a different ID if your clone routine manages that (which means datepicker does not know about the clones).
try updating your js code like this
$clone.find("input.deg_date")
.removeClass('hasDatepicker')
.removeData('datepicker')
.unbind()
.datepicker({
dateFormat: "mm-dd-yy",
changeMonth: true,
yearRange: "-100:+0",
changeYear: true,
maxDate: new Date(),
showButtonPanel: false,
beforeShow: function() {
setTimeout(function() {
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
});
here's the updated JSFIDDLE. hope this helps.
You can re-initiate datepicker,
Put datepicker line at last on click event
$(document).on("click", ".edu_add_button", function () {
...
...
...
...
$(".deg_date").datepicker("setDate", currentDate);
});
It will work!!!