diff --git a/assets/custom-functions.js b/assets/custom-functions.js index bb1dad819e6776932b547caca8534e3ce1865684..76bec1e10f6666f105490c898233a17c455a388c 100644 --- a/assets/custom-functions.js +++ b/assets/custom-functions.js @@ -153,44 +153,46 @@ $(window).on('resize', function () { if (window.innerWidth > 1045) $('.navbar-collapse').removeClass('show') }) -$(document).ready(function () { - $(document).on('click', ".SingleDatePickerInput_clearDate", function() { - var date = document.getElementById('date'); - //ADD SPACES AT APPROPRIATE AREAS FOR DATE SELECTOR - date.addEventListener('input', function(e) { - this.type = 'text'; - var input = this.value; - - //CREATE FUNCTION TO ALLOW DELETION OF SPACES - date.onkeydown = function(event) { - var key = event.keyCode || event.charCode; - inputText = String(input); - if(key == 8 && (input.length==2 || input.length==6)){ - this.value = inputText.substring(0, inputText.length); - return - } - }; - - //ESCAPE FUNCTION IF FINAL CHARACTER IS ENTERED - if(input.length == 11) return; - var values = input.split(' '); - // ADD FUNCTION TO ADD SPACES - var output = values.map(function(v, i) { - // ADD SPACE AFTER FIRST TWO CHARACTERS - if (v.length == 2 && i < 1){ - return v = v + ' '; - // ADD SPACE AFTER SECOND THREE CHARACTERS - }else if (v.length == 3 && i < 2){ - return v = v + ' '; - } else { - return v - } - }); - this.value = output.join('').substr(0, 11); - }); +// ADD SPACES IN DATE INPUT IN BETWEEN DAY MONTH AND YEAR +$(document).ready(function() { + $(document).on('keydown input', '#date', function(e) { + var input = this.value; + var key = e.key || String.fromCharCode(e.keyCode || e.which || e.charCode); + + var caretPosition = this.selectionStart; + + if (key === 'Backspace') { + var previousChar = input[caretPosition - 1]; + + // Allow deletion of spaces + if (previousChar === ' ' && (caretPosition === 3 || caretPosition === 7)) { + this.value = input.slice(0, caretPosition - 2) + input.slice(caretPosition); + this.setSelectionRange(caretPosition - 2, caretPosition - 2); + e.preventDefault(); + return; + } + } + + if (input.length === 11) return; + + var values = input.split(' '); + var output = values.map(function(v, i) { + if (v.length === 2 && i < 1) { + return v + ' '; + } else if (v.length === 3 && i < 2) { + return v + ' '; + } else { + return v; + } }); -}); + this.value = output.join('').substr(0, 11); + + // Adjust caret position after modifying the input value + var newCaretPosition = caretPosition - (input.length - this.value.length); + this.setSelectionRange(newCaretPosition, newCaretPosition); + }); +}); //================== Stats table carets =============================================== // The aeronet stats table changes the position/index of the regions when they