I have a form with a datepicker. The datepicker has a user-facing d/m/Y formatted datepicker input and a hidden altField to go with it for use with the DB.
If the user clears the text in the input field it doesn't clear the altField as well.
I'm using the below JS to get around this problem. Is there a more correct way to do this or is it perfectly acceptable?
$("#datePicker").change(function(){
if ($(this).val().length < 1){
$("#dateAltField").val('');
}
});
What you have works just fine and is a valid approach, alternatively a bit shorter:
$("#datePicker").change(function(){
if (!$(this).val()) $("#dateAltField").val('');
});
According to this bug ticket it's not a bug, it's a feature.
I use this as workaround:
var $input = $('#myInput');
$input.dateinput();
// This is the main part:
$input.on('change', function(){
if (!$input.val()) $input.data('datepicker').settings['altField'].val('');
});
Generalization of mr Lohnisky solution and little flaw fix:
$("body").on("change",".hasDatepicker",function(e) {
if ( !$(this).val() ) {
$( $(this).data("datepicker").settings["altField"] ).val("");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With