(function ($) { const onDOMReady = function () { $('input[type="file"]').each(function () { const $input = $(this), $label = $(`.input__file__text.${$input.attr('id')}`), labelVal = $label.html(); $input.on('change', function (e) { let fileName = ''; if (this.files && this.files.length > 1) fileName = (this.getAttribute('data-multiple-caption') || '{count} files selected').replace( '{count}', this.files.length ); else if (e.target.value) fileName = e.target.value.split('\\').pop(); if (fileName) $label.html( `${fileName}` ); else $label.html(labelVal); }); // Firefox bug fix $input .on('focus', function () { $input.addClass('has-focus'); }) .on('blur', function () { $input.removeClass('has-focus'); }); }); }; $(document).on('ready page:load turbolinks:load', () => { const observer = new MutationObserver(() => onDOMReady()); onDOMReady(); observer.observe(document.body, { childList: true, subtree: true }); }); })(jQuery);