I need help with datepicker on dynamic created input fields...
I have this form:
<table id="reminder">
<tr>
<th>Payment Reminders</th>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<td><label>Reminder</label></td>
<td><input type="text" name="payreminder[]" id="payreminder" class="input1" size="20" ><script>$('#payreminder').datepicker({dateFormat: $.datepicker.W3C, changeMonth: true, changeYear: true, yearRange: '2011:2020'}); </script></td>
<td><label>Description</label></td>
<td><input type="text" class="input1" name="paydescription[]" id="paydescription"></td>
<td> Add another</td>
</tr>
<tr>
<td colspan="6"><div id="dynamicInputReminder"></div> </td>
</tr>
</table>
And the javascript for adding more reminders is:
<script type="text/javascript" >
var counterReminder = 1;
var limit = 20;
function addReminder(divName){
if (counterReminder == limit) {
alert("You have reached the limit of adding " + counterReminder+ " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "<td><label>Reminder</label></td><td><input type=\"text\" id=\"payreminder["+counterReminder+"]\" name=\"payreminder["+counterReminder+"]\" class=\"input1\" ></td><td><label>Description</label></td><td><input type=\"text\" id=\"paydescription["+counterReminder+"]\" name=\"paydescription["+counterReminder+"]\" class=\"input1\"></td>";
document.getElementById(divName).appendChild(newdiv);
counterReminder++;
}
}
</script>
The question is: How and where can I call the script bellow for the date picker so that it works for the dynamically created fields ?
<script>$('#payreminder').datepicker({dateFormat: $.datepicker.W3C, changeMonth: true, changeYear: true, yearRange: '2011:2020'}); </script>
Thanks a lot!
Use a class instead of an id for your input field, for example
<input type=\"text\" id=\"payreminder["+counterReminder+"]\" name=\"payreminder["+counterReminder+"]\" class=\"payreminder\" >
<script>
$(document).on('focusin', '.payreminder', function(){
$(this).datepicker({dateFormat: $.datepicker.W3C, changeMonth: true, changeYear: true, yearRange: '2011:2020'});
});
</script>
try this
remove your script and put this at the end of the code.
NOTE: id should always be unique so i used class here.
change you ids to class "payreminder".
$(document).find('.payreminder').datepicker({dateFormat: $.datepicker.W3C, changeMonth: true, changeYear: true, yearRange: '2011:2020'});
add a class named date on each input field on which you want to add datepicker,
Now each time you add new field, call following function
$(".date").datepicker();
Try this:
<script type="text/javascript" >
var counterReminder = 1;
var limit = 20;
function addReminder(divName) {
if (counterReminder == limit) {
alert("You have reached the limit of adding " + counterReminder+ " inputs");
} else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "<td><label>Reminder</label></td><td><input type=\"text\" id=\"payreminder["+counterReminder+"]\" name=\"payreminder["+counterReminder+"]\" class=\"input1\" ></td><td><label>Description</label></td><td><input type=\"text\" id=\"paydescription["+counterReminder+"]\" name=\"paydescription["+counterReminder+"]\" class=\"input1\"></td>";
document.getElementById(divName).appendChild(newdiv);
$(newDiv).find('input').datepicker({dateFormat: $.datepicker.W3C, changeMonth: true, changeYear: true, yearRange: '2011:2020'});
counterReminder++;
}
}
</script>
$(document).on('focusin', 'input[id^=payreminder]', function(){
$(this).datepicker({dateFormat: $.datepicker.W3C, changeMonth: true, changeYear: true, yearRange: '2011:2020'});
});
Alternatively, you could use a data- attribute for each input instead of a class. The following initializes any datepicker <input type="text" data-datepicker ...> tags as soon as the page is loaded, and then can be called again after the new reminder is added.
function initDatePicker () {
$("input[data-datepicker]").datepicker();
};
$(document).ready(initDatePicker());
Additionally, I've found it to be useful to call the destroy() method for all the datepicker <input>'s each time you reinitialize them; otherwise, they seem to break. I used something like the following:
function addReminder(divName){
...
$("input[data-datepicker]").datepicker('destroy');
initDatePicker();
...
};
Obviously, make sure you call datepicker('destroy') before you call initDatePicker(), or you'll wipe out all your datepickers every time you add a new reminder.
jQueryUI datepicker destroy method
Related
I have dynamic form, Now when Input generated, there is 2 datepicker (FromDate, ToDate), and you can generate many of this tow inputs.
The Input come like this:
<input type="text" class="input-date displayDate complexField" name="Course[FromDate][1]" value="" />
<input type="text" class="input-date displayDate complexField" name="Course[ToDate][1]" value="" />
Each Input will generate jQuery for datepicker like this:
$(document).ready(function() {
{/literal}$(".input-date").datepicker({literal}{
dateFormat: dFormat,
showOn: 'button',
changeMonth: true,
changeYear: true,
minDate: new Date(1940, 1 - 1, 1),
maxDate: '+10y',
yearRange: '-99:+99',
onSelect: function(selected) {
$("[name^='Course[FromDate]']").datepicker("option","maxDate", selected)});
$("[name^='Course[ToDate]']").datepicker("option","minDate", selected)});
}
});
Now everything work fine, you can add many these two inputs and all of them with datepicker.
The Only problem is:
The option onSelect work fine only if you have only the first element (FromDate and ToDate) the ToDate can't be greater than FromDate and vice versa,
But when I add other elemnts which will be:
<input type="text" class="input-date displayDate complexField" name="Course[FromDate][2]" value="" />
<input type="text" class="input-date displayDate complexField" name="Course[ToDate][2]" value="" />
The onSelect will work wrong, it will work 4 together, I need it work with elements with array index (1) alone, and in elements with index number (2) works alone and so on.
Please advice me on this ...
Use this code and try agin
$('document').ready(function(){
$('.input-date').on('click',function(){
$(this).datepicker({literal}{
dateFormat: dFormat,
showOn: 'button',
changeMonth: true,
changeYear: true,
minDate: new Date(1940, 1 - 1, 1),
maxDate: '+10y',
yearRange: '-99:+99',
onSelect: function(selected) {
$("[name^='Course[FromDate]']").datepicker("option","maxDate", selected)});
$("[name^='Course[ToDate]']").datepicker("option","minDate", selected)});
}
});
});
When i submitted form than if error occurred than value is not set in datepicker which is selected before. Datepicker field empty . how this possible ??
<html>
<head>
<script type="text/javascript" >.
$(function() {
$( "#dob" ).datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1900:'
});
$( "#dob" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
});
</script>
</head>
<body>
<form>
<input type="text" name="dob" id="dob" value="<?php if(isset($_SESSION['dob'])) echo $_SESSION['dob']; ?>" placeholder="Please enter date of birth" required/>
</form>
<body>
</html>
There can be many reasons for this issue.
Either the dob variable is null/empty/undefined.
The dob value is not in the correct format.
You have not used defaultDate in the datepicker.
For 1st part, you can only log and debug what is wrong.
For 2nd part, you can format the value in correct format. Search on SO for formatting a string to Date.
For the 3rd part:
$(function () {
$("#dob").datepicker({
defaultDate: $(this).val(), //Add this
dateFormat: 'dd-mm-yy', // And this too
changeMonth: true,
changeYear: true,
yearRange: '1900:'
});
});
I have successfully integrate jQuery DatePicker on my site and wondering how can I set the following:
Don't allow selection of start date from current date plus 2 days. example if today's date is 7/20/12 then the visitor can select only date starting 7/22/12.
End date must start based on Start Date plus one. If start date is 7/23/12 then the end date should be 7/24/12.
BTW, I am making a hotel reservation calendar.
Here's my code based from sample:
<script>
$(function() {
$( "#sd" ).datepicker();
});
$(function() {
$( "#ed" ).datepicker();
});
</script>
<tr>
<td>Check In</td>
<td>:</td>
<td><input name="sd" type="text" id="sd" value="<?php echo $_SESSION['checkin'] ?>" size="10"" maxlength="8" /></td>
</tr>
<tr>
<td>Check Out</td>
<td>:</td>
<td><input name="ed" type="text" id="ed" value="<?php echo $_SESSION['checkout'] ?>" size="10" maxlength="10" /></td>
</tr>
You can do it inside your datepicker events.
$(function() {
$( "#sd" ).datepicker({
// before datepicker opens run this function
beforeShow: function(){
// this gets today's date
var theDate = new Date();
// sets "theDate" 2 days ahead of today
theDate.setDate(theDate.getDate() + 2);
// set min date as 2 days from today
$(this).datepicker('option','minDate',theDate);
},
// When datepicker for start date closes run this function
onClose: function(){
// this gets the selected start date
var theDate = new Date($(this).datepicker('getDate'));
// this sets "theDate" 1 day forward of start date
theDate.setDate(theDate.getDate() + 1);
// set min date for the end date as one day after start date
$('#ed').datepicker('option','minDate',theDate);
}
});
$( "#ed" ).datepicker();
});
please check Jquery: DatePicker: start/end date for answer.
Long story short :
<script type="text/javascript">
$(document).ready(function () {
$('#endDate').datepicker({ showOn: 'button',
buttonImage: '../images/Calendar.png',
buttonImageOnly: true, onSelect: function () { },
onClose: function () { $(this).focus(); }
});
$('#startDate').datepicker({ showOn: 'button',
buttonImage: '../images/Calendar.png',
buttonImageOnly: true, onSelect:
function (dateText, inst) {
$('#endDate').datepicker("option", 'minDate', new Date(dateText));
}
,
onClose: function () { $(this).focus(); }
});
});
can't find solution to this issue. So i have datepicker input field, it works fine, i can choose date and save it to database. But when i want to edit this saved value in edit_profile.php this field is empty, i need to see the user specified date. How to do it?
Datepicker function:
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true
}).val('<?php echo $age;?>');
});
Input:
<input name="datepicker" type="text" id="datepicker" class="date_picker" value="" />
what to do ?
Can't you just do this:-
<input name="datepicker" type="text" id="datepicker" class="date_picker" value="<?php echo $age;?>" >
I think you should use defaultDate option, for example
$( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, defaultDate: yourDate });
You can use SetDate or Defaultdate option of datepicker:
$('#dateselector').datepicker("setDate", new Date(2008,9,03) );
I have a page in which there are multiple input fields that is fromdate and todate. I have created dynamic ids and name. I have used jquery date picker.
Code:
<?php
$i=1;
foreach($locations as $location)
{
?>
<tr>
<td><?php echo $location['name'];?></td>
<td>
<input type="text" name="txtFromDate_<?php echo $location['pk_locationid'];?>" class="field" style="width:80px;" id="txtFromDate_<?php echo $i;?>"/>
</td>
<td>
<input type="text" name="txtToDate_<?php echo $location['pk_locationid'];?>" class="field" style="width:80px;" id="txtToDate_<?php echo $i;?>"/>
</td>
<td>
<input type="text" name="txtFromTime_<?php echo $location['pk_locationid'];?>" class="time-pick field" style="width:80px;" />
</td>
<td>
<input type="text" name="txtToTime_<?php echo $location['pk_locationid'];?>" class="time-pick field" style="width:80px;"/>
</td>
</tr>
<?php
$i++;
}
?>
Jquery code For Date Picker:
(document).ready(function(){
var count=0;
count=<?php echo count($locations);?>;
for(i=1;i<=count;i++)
{
var dates = $('#txtFromDate_'+i+', #txtToDate_'+i).datepicker({
defaultDate: new Date(),
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
onSelect: function(selectedDate) {
var option = this.id == "txtFromDate_"+i ? "maxDate" : "minDate";
var instance = $(this).data("datepicker");
var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
dates.not(this).datepicker("option", option, date);
}
});
}
Jquery Date Picker is displayed for fromdate and to date fields. In the above code i have applied the from date to date validation i.e. to date should be greater than from date.
The problem with above code is that this validation is applied on the last row only.
I want that this validation should be applied to all the rows.
You redeclare the dates variable at every iteration of the loop. So when the loop is done, the dates variable that you're using in the onSelect handler is equal to last item that was set in the loop.
Update Try this code:
Update2 One more thing is the issue of variable i. Its current value is available while in the loop, so later on, when the onSelect handler is used, the i has a value as in last iteration. To overcome this, you have to put i in a context of another function, that's why I have wrapped code in the body of the loop in another function to which I'm passing i variable.
Update3 And one more thing - the logic for picking the option (minDate or maxDate) had to be reversed.
$(document).ready(function(){
var count=0;
count=<?php echo count($locations);?>;
for(i=1;i<=count;i++)
{
(function(i) {
$('#txtFromDate_'+i+', #txtToDate_'+i).datepicker({
defaultDate: new Date(),
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
onSelect: function(selectedDate) {
var option, otherPicker;
if (this.id == "txtFromDate_"+i) {
otherPicker = $("#txtToDate_"+i);
option = "minDate";
} else {
otherPicker = $("#txtFromDate_"+i);
option = "maxDate";
}
var instance = $(this).data("datepicker");
var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
otherPicker.datepicker("option", option, date);
}
};
})(i);
}
});