I am pre-selecting a date on <input type='date' />
and want the user to select a date anyway. If the date selected is the same as the pre-selected value, it does not trigger the change
event in jQuery.
Also tried blur
and input
, but it only activates when losing the focus of the element, not on date selection.
Any ideas how to achieve this?
The pre-selected date is a suggested date which can be chosen or another date could be chosen.
<input id="my-date" type="date" value="2024-07-20" />
// ..
$( '#container' ).on( 'change', '#my-date', function()
{
console.log( 'selected date: ' + $( this ).val() );
});
2
Answers
Instead of using the change event, you should try detecting if the date’s new value is the same as its old value.
To trigger an event when the user selects a date on an , you can handle the click event on the date input even if the chosen date is the same as the pre-selected value. The change event doesn’t fire if the value remains the same, so we must manually trigger our custom event.
Here’s how you can achieve this:
Here is the HTML code:
Here is the jQuery code: