This is my first question.
I have looked for an answer but my searches seem to bring back a lot of ways to enforce the datepicker value is empty, or being able to access the selected value which is not an option here.
Background to the problem
I have a table with a dynamic amount of rows, each row contains a few datepickers (we'll call them A datepicker, B datepicker, C datepicker). It also contains a child row that is hidden until the 'Expand for more details' button is clicked, at which point there are a few more fields appearing. One of those fields is a 'Comments' field.
The requirement I have is that when the A datepicker is changed that a 'Comment' MUST be required to go along with that. I am able to tell when the datepicker is changed and therefore to send a message via the validator of the 'Comments' telling the user that it is now required.
The requirement
When entering details into the comments section, I need to be able to check if there is a date selected in the A datepicker so that we can dynamically warn the user that the comments section needs to be X many characters IF there is a date in the A datepicker.
The problem
It appears that when the jQuery datepicker is changed that the .value is not updated. When the row is populated it is possible that the A datepicker can be empty, not all fields require that particular date to be set and therefore not all 'Comment' fields need to be set either.
So I can tell when the datepicker is changed and get the 'Comments' field to be required. However a user can then fill out the 'Comments' field with a blank entry or enter a value that doesn't meet the X amount of characters limit. This behaviour is OK for when there is not a date selected. So when a date is selected I need to enforce the rule.
My problem is that I can't tell if a date is selected or not!
In the example below I had a blank datepicker, I then chose a date and opened developer tools. In the overview of the web page we can see that the date is set to 20th Jan 2018.
Overview of the web page
however through the developer tools we can see that the value is actually empty.
Datepicker value is set to ""
This behaviour is also true when the datepicker was not initially blank, rather than the value being the new date it still retains the old date. In the example below I changed the date from the 23rd of December 2017 to the 31st of December 2017.
Pre-Populated datepicker
And in developer tools the value is still set to the previous value.
Changed to new date
Technicalities
I am using MVC and jQuery, I am not looking for a solution that involves having to go back to the controller as that usually requires the user having to press a submit button and my requirement is that it needs to be able to respond upon selecting the date and entering comments. I'd just like a way to be able to get the displayed date from the datepicker and not it's previous value.
This matters because if it's going from previously having a date into now a blank date, it affects the requirement so that comments are no longer required as there isn't a date. Vice versa if going from a blank date to a selected date.
Things I have tried
I am trying to write this into the OnChange method of 'Comments', so that I can check if there is a date selected or not.
I have tried directly calling it in my .js file with the below. For option 4 and 5 it's contained in a .each loop that goes through ALL the rows in my table and finds the row relating to the 'Comments' field that was changed.
(Please note that parentRow refers to the selected table row which contains the dates (the row that contains the comments field is the child row)).
1) var dateValue = $(parentRow).find("input[name$='datepicker_A']").datepicker("getDate");
2) var dateValue = $(parentRow).find("input[name$='datepicker_A']").val();
3) var datepicker_A = document.getElementById('datepicker_A');
var dateValue = datepicker_A.val();
4) var dateValue = $.datepicker.parseDate("d/M/y", $(this).find("input[name$='datepicker_A']").val());
5) var dateValue = $(this).find("input[name$='datepicker_A']").val();
All of the above return the previous value of the datepicker and not the newly selected one. So my question is.. how do I get it to recognise the new value?
TL:DR
jQuery datepicker doesn't seem to change the .val() property of the input field when selecting a new date, instead the .val() is always the previous value. How can I access the new value via javascript/jQuery?
Your code should look something like this:
<div>
Select date: <input id="thedate" type="text" class="date-pick" />
</div>
And your JavaScript (I am using jQuery 1.9.1 with jQuery UI 1.9.2 here too)
$(function() {
$('.date-pick').datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(dateText, inst) {
alert(dateText); // Put your code in here
}
});
});
Here's a JsFiddle
Related
I have Vue app that has a date input with masking(dd/mm/yyyy). I am using Maska currently but also tried Inputmask.
Here is the example,
When I fill the input as 22/12/2002 and I delete the part of day (22),
The current behavior:
It pushes the all rest of the input value to left
12/20/02__
Is there any way/a plugin to keep it blank instead of pushing the all input value to left when the user deletes a part of the input?
The ideal behavior:
__/12/2002
I tried InputMask which seemed to have the implementation but could not make it work.
new Inputmask({ alias: 'datetime', inputFormat: 'dd/mm/yyyy', shiftPositions: true });
Also I found actually attribute type="date" to input element works but I need the different date format also the input has some custom style, so it's not really ideal to use it.
Let me know if there is any workaround for these plugins or a plugin that does this behavior.
I'm trying to dynamically change the date picker value
I'm changing the input value using Jquery but unfortunately, date picker value stays the same.
image of the problem
You can see that the initial value is "19/06/2017" and after changing it shows the new value ("10/10/2017") in the text box but date picker still shows the previous date ("19/06/2017")
I tried using $('.datepicker').datepicker('update', $('#doc_due_date').val()); but still nothing changed
Is there a way update or refresh the date picker after changing the input value?
I read some answers regarding the same issue but seems like nothing worked, date picker still shows the initial value
thanks in advance.
p.s
I'm using bootstrap date picker
use this
$('#datepicker_id').datepicker({
onSelect: function () {
$('#data').text(this.value);
}
});
I'm using the UI-Bootstrap Datepicker inline for date selection, as shown below.
<datepicker ng-model="profile.available_from" show-weeks="true"></datepicker>
I would like to enable the user to deselect the currently selected date by clicking the same date again.
Eg. the user clicks 29 May 2015, the corresponding tile is highlighted and profile.available_from is updated with the value. If the user now clicks that same date again, the selection highlight should be removed and profile.available_from is set to undefined/null.
Any ideas?
I guess this is not possible without modifying the original source code of datepicker.
It would have to check if the field already has the selected value, and if yes, then clear it. Which would complicate the code with no obvious benefit.
I would add clear button instead.
example code (taken from http://angular-ui.github.io/bootstrap/#/datepicker)
$scope.clear = function () {
$scope.profile.available_from = null;
};
I am working on a project using Ruby + the Bootstrap framework, including the Bootstrap datetimepicker plugin.
I have a field in my form as thus:
<div class='datetimepicker-input input-group' id='logged_at'>
<input class='form-control' data-format='DD/MM/YYYY hh:mm A' name='logged_at' type='text' value='27/06/2014 05:08 PM'>
<span class='input-group-addon'>
<i class='fa fa-calendar'></i>
</span>
</input>
</div>
As you can see, I want my datetimepicker to use the DD/MM/YYYY hh:mm A formatting, however, when I enter a default date and time in the value attribute of the <INPUT> tag when generating the form, and then click on the calendar icon to pop up the picker, the picker window just gives me NaN in the heading and won't let me select a date or a time.
If however, I leave the value attribute blank when generating the form, the popup works just fine.
I have also noticed that if I type over the default date in the field, and swap the month and day around, i.e. asMM/DD/YYYY hh:mm A,the popup works fine, i.e. the calendar defaults to the right day, however when I choose a date, it displays in the input field as DD/MM/YYYY hh:mm A.
I am wondering if there is another way I have to set the default date/time in the field? Simply populating the value attribute seems to be confusing the plugin, as it appears to be wanting the initial date in mm/dd/yyyy..., but then happy to show it as dd/mm/yyyy... afterwards.
Ok, I think I have figured this one out. It appears to be related to moment.js which bootstrap-datetimepicker relies on for date parsing.
Without messing around with the language options, there was a line in bootstrap-datetimepicker.js (line 222) which had to be changed from:
if (dateStr) picker.date = moment(dateStr);
to
if (dateStr) picker.date = moment(dateStr, picker.format);
Basically we had to use the picker.format as the second parameter to force mention to parse the entered date using the supplied format that we had in the INPUT field.
I am dynamcically generating rows with some controls . In one of those controls i am having a textbox which is binded with datepicker .
Everything is working fine but there is a minor issue i.e when i select date from textbox-2 which is dynamically generated the selected date is apperaring in textbox-1 and textbox-2 is empty sadly .. so this goes on like if i have 10 textboxes in any textbox i select date end of the day i get the selected date in textbox-1 only ..
You can find the excat senario here : http://jsfiddle.net/JL26Z/8/
Correct me if i made any silly error
Regards
there is minor issue with the element, you created element with id so due to same id its causing problem.
please use class instead of id, it will work fine.
I have updated the fiddle, see here:
http://jsfiddle.net/JL26Z/10/
If you have a look at generated html when you add new fields by pressing button you will see that every text box has the same id. When you set date in calendar it searches for text box with matching id and finds first text box on your form and sets date for it. Make sure that you generate unique id for newly added controls. You can have a counter variable and increment it and append to id when you add new set of controls.
In your fiddle calendar shows for phone number field when it should probably show for date field.